Junit使用教程(三)

四、实例总结

1. 参数化测试

有时一个测试方法,不同的参数值会产生不同的结果,那么我们为了测试全面,会把多个参数值都写出来并一一断言测试,这样有时难免费时费力,这是我们便可以采用参数化测试来解决这个问题。参数化测试就好比把一个“输入值,期望值”的集合传入给测试方法,达到一次性测试的目的。

[java] view plaincopy
 
  1. package test;  
  2.   
  3. import static org.junit.Assert.*;  
  4.   
  5. import java.util.Arrays;  
  6.   
  7. import org.junit.Test;  
  8. import org.junit.runner.RunWith;  
  9. import org.junit.runners.Parameterized;  
  10. import org.junit.runners.Parameterized.Parameters;  
  11.   
  12. @RunWith(Parameterized.class)  
  13. public class FibonacciTest {  
  14.   
  15.     @Parameters(name = "{index}: fib({0})={1}")  
  16.     public static Iterable<Object[]> data() {  
  17.         return Arrays.asList(new Object[][] { { 0, 0 }, { 1, 1 }, { 2, 1 },  
  18.                 { 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 } });  
  19.     }  
  20.   
  21.     private int input;  
  22.     private int expected;  
  23.   
  24.     public FibonacciTest(int input, int expected) {  
  25.         this.input = input;  
  26.         this.expected = expected;  
  27.     }  
  28.   
  29.     @Test  
  30.     public void test() {  
  31.         assertEquals(expected, Fibonacci.compute(input));  
  32.     }  
  33. }  
  34.   
  35. class Fibonacci {  
  36.   
  37.     public static int compute(int input) {  
  38.         int result;  
  39.         switch (input) {  
  40.         case 0:  
  41.             result = 0;  
  42.             break;  
  43.         case 1:  
  44.         case 2:  
  45.             result = 1;  
  46.             break;  
  47.         case 3:  
  48.             result = 2;  
  49.             break;  
  50.         case 4:  
  51.             result = 3;  
  52.             break;  
  53.         case 5:  
  54.             result = 5;  
  55.             break;  
  56.         case 6:  
  57.             result = 8;  
  58.             break;  
  59.         default:  
  60.             result = 0;  
  61.         }  
  62.         return result;  
  63.     }  
  64. }  

@Parameters注解参数name,实际是测试方法名称。由于一个test()方法就完成了所有测试,那假如某一组测试数据有问题,那在Junit的结果页面里该如何呈现?因此采用name实际上就是区分每个测试数据的测试方法名。如下图:

2. 打包测试

同样,如果一个项目中有很多个测试用例,如果一个个测试也很麻烦,因此打包测试就是一次性测试完成包中含有的所有测试用例。

[java] view plaincopy
 
  1. package test;  
  2.   
  3. import org.junit.runner.RunWith;  
  4. import org.junit.runners.Suite;  
  5.   
  6. @RunWith(Suite.class)  
  7. @Suite.SuiteClasses({ AssertTests.class, FibonacciTest.class, JDemoTest.class })  
  8. public class AllCaseTest {  
  9.   
  10. }  

这个功能也需要使用一个特殊的Runner ,需要向@RunWith注解传递一个参数Suite.class 。同时,我们还需要另外一个注解@Suite.SuiteClasses,来表明这个类是一个打包测试类。并将需要打包的类作为参数传递给该注解就可以了。至于AllCaseTest随便起一个类名,内容为空既可。运行AllCaseTest类即可完成打包测试

3. 异常测试

异常测试与普通断言测试不同,共有三种方法,其中最为灵活的是第三种,可以与断言结合使用

第一种:

[java] view plaincopy
 
  1. @Test(expected= IndexOutOfBoundsException.class)   
  2. public void empty() {   
  3.      new ArrayList<Object>().get(0);   
  4. }  

第二种:

[java] view plaincopy
 
  1. @Test  
  2. public void testExceptionMessage() {  
  3.     try {  
  4.         new ArrayList<Object>().get(0);  
  5.         fail("Expected an IndexOutOfBoundsException to be thrown");  
  6.     } catch (IndexOutOfBoundsException anIndexOutOfBoundsException) {  
  7.         assertThat(anIndexOutOfBoundsException.getMessage(), is("Index: 0, Size: 0"));  
  8.     }  
  9. }  

第三种:

[java] view plaincopy
 
  1. @Rule  
  2. public ExpectedException thrown = ExpectedException.none();  
  3.   
  4. @Test  
  5. public void shouldTestExceptionMessage() throws IndexOutOfBoundsException {  
  6.     List<Object> list = new ArrayList<Object>();  
  7.   
  8.     thrown.expect(IndexOutOfBoundsException.class);  
  9.     thrown.expectMessage("Index: 0, Size: 0");  
  10.     list.get(0);  
  11.     Assert.assertEquals(1, list.get(0));  
  12. }  

在上述几种方法中,无论是expected还是expect都表示期望抛出的异常,假如某一方法,当参数为某一值时会抛出异常,那么使用第一种方法就必须为该参数单独写一个测试方法来测试异常,而无法与其他参数值一同写在一个测试方法里,所以显得累赘。第二种方法虽然解决这个问题,但是写法不仅繁琐也不利于理解。而第三种犯法,不仅能动态更改期望抛出的异常,与断言语句结合的也非常好,因此推荐使用该方法来测试异常。

4. 限时测试

有时为了防止出现死循环或者方法执行过长(或检查方法效率),而需要使用到限时测试。顾名思义,就是超出设定时间即视为测试失败。共有两种写法。

第一种:

[java] view plaincopy
 
  1. @Test(timeout=1000)  
  2. public void testWithTimeout() {  
  3.   ...  
  4. }  

第二种:

[java] view plaincopy
 
  1. public class HasGlobalTimeout {  
  2.     public static String log;  
  3.   
  4.     @Rule  
  5.     public Timeout globalTimeout = new Timeout(10000); // 10 seconds max per method tested  
  6.   
  7.     @Test  
  8.     public void testInfiniteLoop1() {  
  9.         log += "ran1";  
  10.         for (;;) {  
  11.         }  
  12.     }  
  13.   
  14.     @Test  
  15.     public void testInfiniteLoop2() {  
  16.         log += "ran2";  
  17.         for (;;) {  
  18.         }  
  19.     }  
  20. }  

其中,第二种方法与异常测试的第三种方法的写法类似。也是推荐的写法。

 

至此,Junit的教程总结性文章已介绍完了。通过系统总结也进一步加深了对Junit的认识,希望也能同样帮助到对Junit还不太理解的朋友。如果大家还有什么好的建议和用法,很欢迎能提出来一起交流。

posted on 2015-05-18 11:41  开始、  阅读(337)  评论(0编辑  收藏  举报

导航