Junit hamcrest使用
2011-12-22 22:11
351 查看
本人转载于: http://mgc.name/article.asp?id=811
1.JUnit4.4引入了Hamcrest框架,Hamcest提供了一套匹配符Matcher,这些匹配符更接近自然语言,可读性高,更加灵活;
2.使用全新的断言语法:assertThat,结合Hamcest提供的匹配符,只用这一个方法,就可以实现所有的测试;
3.assertThat语法如下:
assertThat(T actual, Matcher<T> matcher);
assertThat(String reason, T actual, Matcher<T> matcher);
其中actual为需要测试的变量,matcher为使用Hamcrest的匹配符来表达变量actual期望值的声明;
4.注意事项:
a.必须导入JUnit4.4之后的版本才能使用assertThat方法;
b.不需要继承TestCase类,但是需要测试方法前必须加“@Test”。
c.需要导入Hamcrest类库:hamcrest-core-1.3.0RC2.jar | hamcrest-library-1.3.0RC2.jar
3、使用assertThat
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" ) );
1.JUnit4.4引入了Hamcrest框架,Hamcest提供了一套匹配符Matcher,这些匹配符更接近自然语言,可读性高,更加灵活;
2.使用全新的断言语法:assertThat,结合Hamcest提供的匹配符,只用这一个方法,就可以实现所有的测试;
3.assertThat语法如下:
assertThat(T actual, Matcher<T> matcher);
assertThat(String reason, T actual, Matcher<T> matcher);
其中actual为需要测试的变量,matcher为使用Hamcrest的匹配符来表达变量actual期望值的声明;
4.注意事项:
a.必须导入JUnit4.4之后的版本才能使用assertThat方法;
b.不需要继承TestCase类,但是需要测试方法前必须加“@Test”。
c.需要导入Hamcrest类库:hamcrest-core-1.3.0RC2.jar | hamcrest-library-1.3.0RC2.jar
3、使用assertThat
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" ) );
相关文章推荐
- junit和hamcrest的简单使用
- 学习junit和hamcrest的使用
- Junit hamcrest使用
- 使用Hamcrest增强JUnit的测试能力
- JUnit: Hamcrest 使用
- Junit中使用hamcrest
- JUnit-4.11使用报java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing错误 .
- JUnit: Hamcrest 使用
- junit搭配hamcrest使用
- JUnit: Hamcrest 使用
- 软件测试之实验一——如何使用JUnit,Hamcrest和Eclemma进行简单的软件测试
- JUnit中使用Hamcrest测试框架的assertThat断言
- 使用Hamcrest增强JUnit的测试能力
- Junit hamcrest使用
- 软件测试(三)——Junit、Hamcrest、Eclemma的安装和使用
- 使用Ant运行JUnit测试用例时,报java.lang.ClassNotFoundException: org.hamcrest.SelfDescribing的一种解决方案
- JUnit: Hamcrest使用
- 解决“java.lang.NoSuchMethodError: org.hamcrest.core.AllOf.allOf…”问题(使用JUnit和Hamcrest)
- [软件测试_LAB1]安装junit和hamcrest及其使用
- 使用junit单元测试的java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing问题解决办法