二、Junit4 注解

Junit4 注解提供了书写单元测试的基本功能。.本章将介绍@BeforeClass, @AfterClass,@Before, @After 和@Tes 这几个基本t注解。

@BeforeClass注解

被@BeforeClass注解的方法会是:
  • 只被执行一次
  • 运行junit测试类时第一个被执行的方法
这样的方法被用作执行计算代价很大的任务,如打开数据库连接。被@BeforeClass 注解的方法应该是静态的(即 static类型的).

@AfterClass注解

被@AfterClass注解的方法应是:

  • 只被执行一次
  • 运行junit测试类是最后一个被执行的方法
该类型的方法被用作执行类似关闭数据库连接的任务。被@AfterClass 注解的方法应该是静态的(即 static类型的).

@Before注解

被@Before 注解的方法应是:
  • junit测试类中的任意一个测试方法执行 前 都会执行此方法
该类型的方法可以被用来为测试方法初始化所需的资源。

@After注解

被@After注解的方法应是:
  • junit测试类中的任意一个测试方法执行后 都会执行此方法, 即使被@Test 或 @Before修饰的测试方法抛出异常
该类型的方法被用来关闭由@Before注解修饰的测试方法打开的资源。

@Test 注解

被@Test注解的测试方法包含了真正的测试代码,并且会被Junit应用为要测试的方法。@Test注解有两个可选的参数:
  • expected 表示此测试方法执行后应该抛出的异常,(值是异常名)
  • timeout 检测测试方法的执行时间
<h3 style="background-color:rgb(153,204,255);""> Junit4注解例子

Arithmetic.Java,本例要用到的需要Junit进行单元测试的类:

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-family:Microsoft YaHei;font-size:12px;">package in.co.javatutorials;  
  2.    
  3. public class Arithmetic {  
  4.    
  5.     public int add(int i, int j) {  
  6.         return i + j;  
  7.     }  
  8. }</span>  
ArithmeticTest.java  Arithmetic.java的Junit单元测试类:
[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-family:Microsoft YaHei;font-size:12px;">package in.co.javatutorials;  
  2.    
  3. import static org.junit.Assert.assertEquals;  
  4.    
  5. import org.junit.After;  
  6. import org.junit.AfterClass;  
  7. import org.junit.Before;  
  8. import org.junit.BeforeClass;  
  9. import org.junit.Test;  
  10.    
  11. public class ArithmeticTest {  
  12.    
  13.     @BeforeClass  
  14.     public static void setUpClass() {  
  15.         // one time setup method  
  16.         System.out.println("@BeforeClass - executed only one time and is first method to be executed");  
  17.     }  
  18.    
  19.     @AfterClass  
  20.     public static void tearDownClass() {  
  21.         // one time tear down method  
  22.         System.out.println("@AfterClass - executed only one time and is last method to be executed");  
  23.     }  
  24.    
  25.     @Before  
  26.     public void setUp() {  
  27.         // set up method executed before every test method  
  28.         System.out.println("@Before - executed before every test method");  
  29.     }  
  30.    
  31.     @After  
  32.     public void tearDown() {  
  33.         // tear down method executed after every test method  
  34.         System.out.println("@After - executed after every test method");  
  35.     }  
  36.    
  37.     @Test  
  38.     public void testAdd() {  
  39.         Arithmetic arithmetic = new Arithmetic();  
  40.         int actualResult = arithmetic.add(34);  
  41.         int expectedResult = 7;  
  42.         assertEquals(expectedResult, actualResult);  
  43.         System.out.println("@Test - defines test method");  
  44.     }  
  45. }</span>  

样例结果输出

本例在Eclipse Junit窗口的输出结果如下:


样例日志输出:

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-family:Microsoft YaHei;font-size:12px;">@BeforeClass - executed only one time and is first method to be executed  
  2. @Before - executed before every test method  
  3. @Test - defines test method  
  4. @After - executed after every test method  
  5. @AfterClass - executed only one time and is last method to be executed</span>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值