JUnit4 新特性

JUnit 4是JUnit框架有史以来的最大改进,其主要目标便是利用Java 5的Annotation特性简化测试用例的编写。让我们看看如何使用JUnit 4来进行Unit测试。

请注意:本文主要介绍JUnit 4的最新特性和用法,并不会讲解Unit测试的基础。如果你对JUnit框架还不熟悉,请先参考“在Eclipse中使用JUnit”一文,学习如何编写JUnit测试。

我们使用的开发环境是Eclipse 3.2,它已经自带了JUnit 4.1,你需要将JUnit 4 Library添加到项目用到的Library中。另外,必须使用JDK 5.0或更高版本。

要在Eclipse环境之外运行JUnit,需要下载JUnit 4.1,具体请访问JUnit.org。

我们先看一个简单的Math类:

ActionScript/Java代码
  1. package com.crackj2ee.junit4;  
  2. public class Math {  
  3.   
  4.    public int abs(int value) {  
  5. return value>=0 ? value : (-value);  
  6.    }  
  7.   
  8.    public int div(int a, int b) {  
  9. return a / b;  
  10.    }  
  11.   
  12.    /** 
  13.    * BUG: if b less than 0! 
  14.    */  
  15.    public float exp(int a, int b) {  
  16. float r = 1;  
  17. for(int i=0; i<b; i++)  
  18.        r = r * a;  
  19. return r;  
  20.    }  
  21. }  



注意exp()方法是有Bug的,如果传入参数2, -1,则期待的返回值应为0.5F,但实际返回值为1.0F。

下面我们看看传统的JUnit的TestCase:

ActionScript/Java代码
  1. public class MathTest extends TestCase {  
  2.   
  3.    public void setUp() { super.setUp(); }  
  4.    public void tearDown() { super.tearDown(); }  
  5.   
  6.    public void testAbs() { assertTrue(true); }  
  7.    public void testDiv() {...}  
  8.    public void testExp() {...}  
  9.   
  10. }  


JUnit依赖反射来执行每个以test开头的方法。然而,在最新的JUnit 4中,由于有了Annotation的支持,我们的测试方法不需要再以testXxx标识了,而是写上一个@Test标注即可。例如:

@Test public void doAbs() {...}
甚至MathTest类也不必继承自TestCase。你也许会想到,不继承自TestCase就无法调用assertXxx方法了,正因为如此,所有的 assertXxx方法全部以静态方法被放入了Assert类,使用Assert.assertXxx()调用。如果使用

import static org.junit.Assert.*;
则原有的代码不必改动。

setUp()和tearDown()方法也依赖@Before和@After标记,这样做的最大的好处是在继承体系内不必担心忘记了在setUp()方 法中调用父类的super.setUp()方法,JUnit框架会自动处理父类的@Before和@After标记的方法。

并且,JUnit框架对@Before和@After的调用顺序类似于类的构造方法和析构方法,即@Before按照父类到子类的顺序调用,@After则相反,这样保证了资源的正确获取和释放。

当然,不再强迫必须使用setUp和tearDown作为方法名,可以使用更有意义的方法名,例如:initDatabase()和closeDatabase(),只要它们被标注了@Before和@After即可。

来看看使用Annotation的MathTest:

ActionScript/Java代码
  1. package com.crackj2ee.junit4;  
  2.   
  3. import static org.junit.Assert.*;  
  4.   
  5. import org.junit.*;  
  6.   
  7. public class MathTest  
  8. {  
  9.   
  10.     public MathTest()  
  11.     {  
  12.         System.out.println("new MathTest instance.");  
  13.     }  
  14.   
  15.     @Before public void setUp()throws Exception  
  16.     {  
  17.         System.out.println("call @Before before a test method");  
  18.     }  
  19.   
  20.     @After public void tearDown()throws Exception  
  21.     {  
  22.         System.out.println("call @After after a test method");  
  23.     }  
  24.   
  25.     @Test public void doAbs()  
  26.     {  
  27.         Math math = new Math();  
  28.         assertEquals(200, math.abs(200));  
  29.         assertEquals(100, math.abs( - 100));  
  30.         assertEquals(0, math.abs(0));  
  31.     }  
  32.   
  33.     @Test public void doDiv()  
  34.     {  
  35.         Math math = new Math();  
  36.         assertEquals(5, math.div(10020));  
  37.         assertEquals(4, math.div(10021));  
  38.     }  
  39.   
  40.     @Test(expected = ArithmeticException.class)public void doDiv0()  
  41.     {  
  42.         new Math().div(1270);  
  43.     }  
  44.   
  45.     @Test(timeout = 1)public void doLongTimeTask()  
  46.     {  
  47.         double d = 0;  
  48.         for (int i = 1; i < 10000000; i++)  
  49.             d += i;  
  50.     }  
  51.   
  52.     @Test public void testExp()  
  53.     {  
  54.         Math math = new Math();  
  55.         assertEquals(32f, math.exp(25), 0.001f);  
  56.         assertEquals(1f, math.exp(20), 0.001f);  
  57.         assertEquals(0.5f, math.exp(2, ( - 1)), 0.001f);  
  58.     }  
  59.   
  60. }  


对测试异常,JUnit 4可以用expected=Exception.class来期待一个预期的异常,而不必编写

ActionScript/Java代码
  1. try {  
  2.     ...  
  3.     fail("No exception");  
  4. }  
  5.     catch(Exception e) {  
  6.     // OK!  
  7. }  


来看看doDiv0测试,我们期待一个除数为0的ArithmeticException,因此编写如下测试方法:

ActionScript/Java代码
  1. @Test(expected=ArithmeticException.class)  
  2. public void doDiv0() {  
  3.    new Math().div(1270);  
  4. }  


对于非常耗时的测试,@Test还有一个timeout来标识该方法最长执行时间,超过此时间即表示该测试方法失败:

ActionScript/Java代码
  1. @Test(timeout=1)  
  2. public void doLongTimeTask() {  
  3.    double d = 0;  
  4.    for(int i=1; i<10000000; i++)  
  5.       d+=i;  
  6. }  


以上方法若执行时间超过1ms则测试失败,由于依赖CPU的执行速度,在不同的机器上测试结果也不同。

JUnit 4另一个较大的变化是引入了@BeforeClass和@AfterClass,它们在一个Test类的所有测试方法执行前后各执行一次。这是为了能在 @BeforeClass中初始化一些昂贵的资源,例如数据库连接,然后执行所有的测试方法,最后在@AfterClass中释放资源。

正如你能想到的,由于@BeforeClass和@AfterClass仅执行一次,因此它们只能标记静态方法,在所有测试方法中 共享的资源也必须是静态引用:

ActionScript/Java代码
  1. private static Object dbConnection;  
  2.   
  3. @BeforeClass  
  4. public static void setUpBeforeClass() throws Exception {  
  5.    System.out.println("call @BeforeClass and init database connection");  
  6.    dbConnection = new Object();  
  7. }  
  8.   
  9. @AfterClass  
  10. public static void tearDownAfterClass() throws Exception {  
  11.    System.out.println("call @AfterClass to release database connection");  
  12.    dbConnection = null;  
  13. }  


最后执行测试用例,可以看到结果:

各个方法执行顺序如下:

call @BeforeClass and init database connection

new MathTest instance.
call @Before before a test method
call @After after a test method

new MathTest instance.
call @Before before a test method
call @After after a test method

...

call @AfterClass to release database connection

可以看到,@BeforeClass是在实例化MathTest之前调用的,因此不能在构造方法中初始化共享资源。

最后需要注意的是由于Java 5的自动Box/Unbox特性,在调用assertEquals()时要特别注意,如果你传入:

assertEquals(100F, 100);

则按照自动Box变为:

assertEquals(new Float(100F), new Integer(100));

测试失败,因为Float类和Integer类不是同一类型。

因此要特别注意float和double的测试。事实上对float和double应使用

assertEquals(float, float, float delta);
assertEquals(double, double, double delta);

delta指定了两个作比较的浮点数的相差范围,在此范围内的两个浮点数将认为相等。可以传入一个很小的数例如0.0001F。 

JUnit 4非常适合使用Java 5的开发人员,但是无法在Java 1.4中获得这些好处,并且,也不与以前的版本兼容。因此,如果你正在使用Java 5,就可以考虑使用JUnit 4来编写测试。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值