liwx2000 发表于 2009-11-23 22:36:18

发一些JUnit4中hamcrest断言的方法

assertThat( n, allOf( greaterThan(1), lessThan(10) ) );//是否既大于1又小于10
assertThat( n, anyOf( greaterThan(1), lessThan(10) ) );//是否大于1或者小于10
assertThat( n, anything() );//n是任意值都测试通过
assertThat( str, is( "test" ) );//str是否为test
assertThat( str, not( "test" ) );//str是否不为test
assertThat( str, containsString( "test" ) );//str是否包含test
assertThat( str, endsWith("test" ) );   //str是否以test结尾
assertThat( str, startsWith( "test" ) );   //str是否以test开头
assertThat( n, equalTo( nExpected ) );   //n是否与另外一个n相等
assertThat( str, equalToIgnoringCase( "test" ) );   //str是否与test相等(忽略大小写)
assertThat( str, equalToIgnoringWhiteSpace( "test" ) );   //str是否与test相等(忽略空格)
assertThat( d, closeTo( 3.0, 0.3 ) );   //d是否接近3.0,误差不超过0.3
assertThat( d, greaterThan(3.0) );    //是否大于3.0
assertThat( d, lessThan (10.0) );//是否小于10.0
assertThat( d, greaterThanOrEqualTo (5.0) );//是否大于等于5.0
assertThat( d, lessThanOrEqualTo (16.0) );//是否小于等于16.0
assertThat( map, hasEntry( "test", "test" ) );//map中是否有test(key)对应的test(value)
assertThat( iterable, hasItem ( "test" ) );//list中是否包含test对象
assertThat( map, hasKey ( "test" ) );//map中是否包含test(key)
assertThat( map, hasValue ( "test" ) );//map中是否包含test(value)

小不点蜗牛 发表于 2009-12-27 13:47:39

不错啊

kaidong12 发表于 2010-3-21 18:53:15

不错,这个东西好像没有Help doc
页: [1]
查看完整版本: 发一些JUnit4中hamcrest断言的方法