Junit--assertthat (Acture,matcher)

Source: Internet
Author: User

The relevant jar packages, including Hamcrest-core.1.3.jar and Hamcrest-library-1.3.jar, need to be introduced before using Hamcrest.

specifically introduced by: right-click the JUnit project--build path--add External Archives
at this time in the real test, also need to introduce static method import static org.hamcrest.matchers.*;
Package com.bjsxt.junit4.test;

import static org.junit.assert.*;
import static org.hamcrest.matchers.*;

import org.junit.Test;

import com.bjsxt.junit4.t;

Public class TTest {

@Test
Public void Testadd () {
int z = new T (). Add (5,3);
Assertthat (Z,is (8));
     }

}
after the operation will be error:
Java.lang. SecurityException: Class "Org.hamcrest.Matchers" ' s signer

Reason:the class loader of the JUNIT4 and hamcrest packages are not the same

the solutions are as follows:
(1) Delete the jar package inside the JUnit package, which is the eclipse's own library:junit4--right-click Build Path--remove
(2) Introduction of the latest version of the Library's jar package   1.assertThat
2. Matching method using Hamcrest
a) More natural
3. Example
a) assertthat (n, AllOf (GreaterThan (1), LessThan ()));
assertthat (N, AnyOf (GreaterThan (+), LessThan (8)));
assertthat (n, anything ());
Assertthat (str, is ("BJSXT"));//string is not "BJSXT"
assertthat (str, not ("bjxxt"));

b) assertthat (str, containsstring ("BJSXT"));
assertthat (str, endsWith ("BJSXT"));
assertthat (str, startsWith ("BJSXT"));
assertthat (N, Equalto (nexpected));
assertthat (str, equaltoignoringcase ("BJSXT")); String ignores case equality
assertthat (str, equaltoignoringwhitespace ("BJSXT")); String ignores whether spaces are equal

c) Assertthat (d, Closeto (3.0, 0.3));//value close to 3.0 error not exceeding 0.3
Assertthat (d, GreaterThan (3.0));
Assertthat (d, LessThan (10.0));
Assertthat (d, Greaterthanorequalto (5.0));//greater than or equal to
Assertthat (d, Lessthanorequalto (16.0));

d) assertthat (map, Hasentry ("BJSXT", "BJSXT"));//map inside Key-value Judge
assertthat (iterable, Hasitem ("BJSXT"));
assertthat (Map, Haskey ("BJSXT"));
assertthat (Map, HasValue ("BJSXT"));           

Junit--assertthat (Acture,matcher)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.