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)