下载hamcrest-all-1.3.jar导入到工程中,编写下面的方法进行测试:
package com.sunft.junit4.test;
import static org.junit.Assert.*;
import static org.hamcrest.Matchers.*;
import org.junit.Test;
import com.sunft.junit4.T;
public class TTest {
@Test
public void testAdd() {
int z = new T().add(5, 3);
//这里使用的是hamcrest的匹配模式,is等同于Matcers.is
assertThat(z, is(8));
}
}
运行,报下面的错误:
java.lang.SecurityException: class"org.hamcrest.Matchers"'s signer information does not match signerinformation of other classes in the same package
atjava.lang.ClassLoader.checkCerts(ClassLoader.java:898)
atjava.lang.ClassLoader.preDefineClass(ClassLoader.java:668)
atjava.lang.ClassLoader.defineClass(ClassLoader.java:761)
atjava.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
atjava.net.URLClassLoader.defineClass(URLClassLoader.java:467)
atjava.net.URLClassLoader.access$100(URLClassLoader.java:73)
atjava.net.URLClassLoader$1.run(URLClassLoader.java:368)
atjava.net.URLClassLoader$1.run(URLClassLoader.java:362)
atjava.security.AccessController.doPrivileged(Native Method)
atjava.net.URLClassLoader.findClass(URLClassLoader.java:361)
atjava.lang.ClassLoader.loadClass(ClassLoader.java:424)
atsun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
atjava.lang.ClassLoader.loadClass(ClassLoader.java:357)
atcom.sunft.junit4.test.TTest.testAdd(TTest.java:17)
atsun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
atsun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
atsun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
atjava.lang.reflect.Method.invoke(Method.java:498)
atorg.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
atorg.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
atorg.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
atorg.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
atorg.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
atorg.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
atorg.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
atorg.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
atorg.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
atorg.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
atorg.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
atorg.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
atorg.junit.runners.ParentRunner.run(ParentRunner.java:309)
atorg.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
atorg.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
atorg.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
atorg.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
atorg.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
atorg.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
出现上述错误的原因是Junit和hamcrest使用的ClassLoader并不是同一个,如果使用同一个就不会出问题了。解决方式是删除MyEclipse自带的JUnit包,使用外部引用的方式引入下载好的junit-4.12.jar包,再次进行测试,测试结果正常。
类似于上述is的用法还有以下的示例:
a)assertThat( n, allOf( greaterThan(1), lessThan(15) ) );
assertThat( n, anyOf( greaterThan(16), lessThan(8) ) );
assertThat( n, anything() );//任何值都可以
assertThat( str, is( "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" ) );
assertThat( str, equalToIgnoringWhiteSpace( "bjsxt" ) );
c)assertThat( d, closeTo( 3.0, 0.3 ) );
assertThat( d, greaterThan(3.0) );
assertThat( d, lessThan (10.0) );
assertThat( d, greaterThanOrEqualTo (5.0) );
assertThat( d, lessThanOrEqualTo (16.0) );
d)assertThat( map, hasEntry( "bjsxt", "bjsxt" ) );
assertThat( iterable, hasItem ( "bjsxt" ) );
assertThat( map, hasKey ( "bjsxt" ) );
assertThat( map, hasValue ( "bjsxt" ) );
现在演示一个的例子:
package com.sunft.junit4.test;
import static org.junit.Assert.*;
import static org.hamcrest.Matchers.*;
import org.junit.Test;
import com.sunft.junit4.T;
public class TTest {
@Test
public void testAdd() {
int z = new T().add(5, 3);
//这里使用的是hamcrest的匹配模式,is等同于Matcers.is
assertThat(z, is(8));
//演示allOf的使用
assertThat(z, allOf(greaterThan(5), lessThan(10)));
}
}