Junit

JUnit4是JUnit框架有史以来的最大改进,其主要目标便是利用Java5的Annotation特性简化测试用例的编写。
方法注解:

@BeforeClass:
使用了该元数据的方法在所有测试方法执行前只执行一次。
@AfterClass:
使用了该元数据的方法在所有测试方法执行后只执行一次。

@Before:

使用了该元数据的方法在每个测试方法执行之前都要执行一次。
@After:
使用了该元数据的方法在每个测试方法执行之后要执行一次。
@Test
用了该元数据的方法是测试方法。没有参数和返回值。
限时测试(timeout = 1000)
异常测试(expected = ArithmeticException.class)

@ignore:
该元数据标记的测试方法在测试中会被忽略。

类注解:
@RunWith

参数化测试

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. import static org.junit.Assert.assertEquals;  
  2. import org.junit.Test;  
  3. import org.junit.runner.RunWith;  
  4. import org.junit.runners.Parameterized;  
  5. import org.junit.runners.Parameterized.Parameters;  
  6. import java.util.Arrays;  
  7. import java.util.Collection;  
  8.   
  9. @RunWith(Parameterized.class)  
  10. public class SquareTest ...{  
  11.     private static Calculator calculator = new Calculator();  
  12.     private int param;  
  13.     private int result;      
  14.   
  15.     @Parameters     
  16.     public static Collection data() ...{  
  17.             return Arrays.asList(new Object[][]{  
  18.                 {24},  
  19.                 {00},  
  20.                 {-39},  
  21.         });  
  22. }  
  23.   
  24.     //构造函数,对变量进行初始化  
  25.     public SquareTest(int param, int result){  
  26.         this.param = param;  
  27.             this.result = result;  
  28.   
  29.     }  
  30.   
  31.     @Test     
  32.     public void square(){  
  33.             calculator.square(param);  
  34.             assertEquals(result, calculator.getResult());  
  35.     }  
  36.   
  37. }  
打包测试:
[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. import org.junit.runner.RunWith;  
  2. import org.junit.runners.Suite;  
  3.   
  4. @RunWith(Suite.class)  
  5. @Suite.SuiteClasses(...{CalculatorTest.class, SquareTest.class})  
  6. public class AllCalculatorTests{}  

性能测试:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public class Test {  
  2.     @Rule  
  3.     public ContiPerfRule i = new ContiPerfRule();       
  4.       
  5.     @Test  
  6.     @PerfTest(invocations = 100, threads = 2)  
  7.     @Required(average = 25, percentile95 = 1000)  
  8.     public void test() throws Exception {  
  9.       
  10.     }  
  11. }  
Mokito:

http://docs.mockito.googlecode.com/hg/org/mockito/Mockito.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值