assertThat方法需要使用hamcrest的匹配方法: 示例 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" ) ); assertThat( str, containsString( "bjsxt" ) ); assertThat( str, endsWith("bjsxt" ) ); assertThat( str, startsWith( "bjsxt" ) ); assertThat( n, equalTo( nExpected ) ); assertThat( str, equalToIgnoringCase( "bjsxt" ) ); assertThat( str, equalToIgnoringWhiteSpace( "bjsxt" ) ); 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) ); assertThat( map, hasEntry( "bjsxt", "bjsxt" ) ); assertThat( iterable, hasItem ( "bjsxt" ) ); assertThat( map, hasKey ( "bjsxt" ) ); assertThat( map, hasValue ( "bjsxt" ) ); Failure和Error Failure是指测试失败 Error是指测试程序本身出错 JUnit4 Annotation @Test: 测试方法 (expected=XXException.class) (timeout=xxx) @Ignore: 被忽略的测试方法 @Before: 每一个测试方法之前运行 @After: 每一个测试方法之后运行 @BeforeClass: 所有测试开始之前运行 //必须定义为static方法 @AfterClass: 所有测试结束之后运行 //必须定义为static方法