junit比起和main函数写的测试来说,有一下优点:

1.测试和功能分开,比较明了。

2.自动化,junit可以一次运行test包下面的所有测试,如果加上ant会更好。

3.测试的配置比较多,exception和timeout等标签。

 

junit的命名建议:

放在test包中,类以classnameTest命名,方法以TestMethod命名。

 

junit4使用说明

junit4使用说明

使用junit4,引入hamcrest包:一个core包,一个library包。

如果有错的话,应该将junit4的包删除掉,使用从网上下载的junit包,使用all包。

  1. 使用hamcrest的匹配方法

  a) 更自然

  1. 示例

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. Failure是指测试失败
  2. Error是指测试程序本身出错

junit4使用说明

运行多个测试

junit4使用说明

junit4使用说明

相关文章:

  • 2021-09-13
  • 2022-12-23
  • 2021-12-20
  • 2021-09-21
  • 2022-01-19
  • 2021-12-19
猜你喜欢
  • 2022-01-04
  • 2021-05-30
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-30
  • 2021-04-29
相关资源
相似解决方案