junit4初级
什么是单元测试
写了个类,要给别人用,会不会有bug?怎么办?测试一下。
用main方法测试好不好?不好!
- 不能一起运行!
- 大多数情况下需要人为的观察输出确定是否正确
为什么要进行单元测试
重用测试,应付将来的实现的变化。
提高士气,明确知道我的东西是没问题的。
JUnit4 HelloWorld
- new project
- 建立类
- 建立testcase
package com.bjsxt.junit4.test; import static org.junit.Assert.*; import org.junit.Test; import com.bjsxt.junit4.T; import static org.hamcrest.Matchers.*; public class TTest { @Test public void testAdd() { int z=new T().add(5, 3); assertThat(z, is(8)); } }
放弃旧的断言,使用hamcrest断言
1.assertThat
2.使用hamcrest的匹配方法
a) 更自然
3.示例
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
1.@Test: 测试方法
a) (expected=XXException.class)
b) (timeout=xxx)
package com.bjsxt.junit4.test; import static org.junit.Assert.*; import org.junit.Test; import com.bjsxt.junit4.T; import static org.hamcrest.Matchers.*; public class TTest { @Test public void testAdd() { int z=new T().add(5, 3); assertThat(z, is(8)); } @Test(expected=java.lang.ArithmeticException.class,timeout=100)//抛出异常,并设置时间超过100毫秒就算失败 public void testDivide(){ int z=new T().divide(8, 0); } }
2.@Ignore: 被忽略的测试方法,不执行此测试方法
3.@Before: 每一个测试方法之前运行,在执行每个测试方法之前都会执行before
4.@After: 每一个测试方法之后运行
5.@BeforeClass: 所有测试开始之前运行,在需要搭建或初始化比较耗时的环境时可以用到。
6.@AfterClass: 所有测试结束之后运行
package com.bjsxt.junit4.test; import static org.junit.Assert.*; import static org.hamcrest.Matchers.*; import org.junit.After; import org.junit.Before; import org.junit.BeforeClass; import org.junit.AfterClass; import org.junit.Ignore; import org.junit.Test; import com.bjsxt.junit4.T; public class TTest { @BeforeClass public static void beforeClass() {//static方法 System.out.println("beforeClass"); } @AfterClass public static void afterClass() { System.out.println("afterClass"); } @Before public void before() { System.out.println("before"); } @Test public void testAdd() { int z = new T().add(5, 3); assertThat(z, is(8)); assertThat(z ,allOf(greaterThan(5), lessThan(10))); //int a = 8/0; } @Test(expected=java.lang.ArithmeticException.class, timeout=100) public void testDivide() { int z = new T().divide(8, 0); } @After public void after() { System.out.println("after"); } }
结果:
运行多个测试
注意
- 遵守约定,比如:
a) 类放在test包中
b) 类名用XXXTest结尾
c) 方法用testMethod命名