JUnit4中@Before、@After、@Test等注解的作用

JUnit4使用Java5中的注解(annotation),以下是JUnit4常用的几个annotation: 
@Before:初始化方法   对于每一个测试方法都要执行一次(注意与BeforeClass区别,后者是对于所有方法执行一次)
@After:释放资源  对于每一个测试方法都要执行一次(注意与AfterClass区别,后者是对于所有方法执行一次)
@Test:测试方法,在这里可以测试期望异常和超时时间 
@Test(expected=ArithmeticException.class)检查被测方法是否抛出ArithmeticException异常 
@Ignore:忽略的测试方法 
@BeforeClass:针对所有测试,只执行一次,且必须为static void 
@AfterClass:针对所有测试,只执行一次,且必须为static void 
一个JUnit4的单元测试用例执行顺序为: 
@BeforeClass -> @Before -> @Test -> @After -> @AfterClass; 
每一个测试方法的调用顺序为: 

@Before -> @Test -> @After; 

  1. public class JUnit4Test {     
  2.     @Before    
  3.     public void before() {     
  4.         System.out.println(”@Before”);     
  5.     }     
  6.       
  7.     @Test    
  8.          /**   
  9.           *Mark your test cases with @Test annotations.    
  10.           *You don’t need to prefix your test cases with “test”.   
  11.           *tested class does not need to extend from “TestCase” class.   
  12.           */    
  13.     public void test() {     
  14.         System.out.println(”@Test”);     
  15.         assertEquals(5 + 510);     
  16.     }     
  17.       
  18.     @Ignore    
  19.     @Test    
  20.     public void testIgnore() {     
  21.         System.out.println(”@Ignore”);     
  22.     }     
  23.       
  24.     @Test(timeout = 50)     
  25.     public void testTimeout() {     
  26.         System.out.println(”@Test(timeout = 50)”);     
  27.         assertEquals(5 + 510);     
  28.     }     
  29.       
  30.     @Test(expected = ArithmeticException.class)     
  31.     public void testExpected() {     
  32.         System.out.println(”@Test(expected = Exception.class)”);     
  33.         throw new ArithmeticException();     
  34.     }     
  35.       
  36.     @After    
  37.     public void after() {     
  38.         System.out.println(”@After”);     
  39.     }     
  40.       
  41.     @BeforeClass    
  42.     public static void beforeClass() {     
  43.         System.out.println(”@BeforeClass”);     
  44.     };     
  45.       
  46.     @AfterClass    
  47.     public static void afterClass() {     
  48.         System.out.println(”@AfterClass”);     
  49.     };     
  50. };    
public class JUnit4Test {   
    @Before  
    public void before() {   
        System.out.println("@Before");   
    }   

    @Test  
         /**  
          *Mark your test cases with @Test annotations.   
          *You don’t need to prefix your test cases with “test”.  
          *tested class does not need to extend from “TestCase” class.  
          */  
    public void test() {   
        System.out.println("@Test");   
        assertEquals(5 + 5, 10);   
    }   

    @Ignore  
    @Test  
    public void testIgnore() {   
        System.out.println("@Ignore");   
    }   

    @Test(timeout = 50)   
    public void testTimeout() {   
        System.out.println("@Test(timeout = 50)");   
        assertEquals(5 + 5, 10);   
    }   

    @Test(expected = ArithmeticException.class)   
    public void testExpected() {   
        System.out.println("@Test(expected = Exception.class)");   
        throw new ArithmeticException();   
    }   

    @After  
    public void after() {   
        System.out.println("@After");   
    }   

    @BeforeClass  
    public static void beforeClass() {   
        System.out.println("@BeforeClass");   
    };   

    @AfterClass  
    public static void afterClass() {   
        System.out.println("@AfterClass");   
    };   
};  

输出结果: 
@BeforeClass 
@Before 
@Test(timeout = 50) 
@After 
@Before 
@Test(expected = Exception.class) 
@After 
@Before 
@Test 
@After 
@AfterClass 


@BeforeClass and @AfterClass@Before and @After
在一个类中只可以出现一次

在一个类中可以出现多次,即可以在多个方法的声明前加上这两个Annotaion标签,执行顺序不确定

方法名不做限制方法名不做限制
在类中只运行一次在每个测试方法之前或者之后都会运行一次

@BeforeClass父类中标识了该Annotation的方法将会先于当前类中标识了该Annotation的方法执行。
@AfterClass 父类中标识了该Annotation的方法将会在当前类中标识了该Annotation的方法之后执行

@Before父类中标识了该Annotation的方法将会先于当前类中标识了该Annotation的方法执行。
 @After父类中标识了该Annotation的方法将会在当前类中标识了该Annotation的方法之后执行
必须声明为public static必须声明为public 并且非static
所有标识为@AfterClass的方法都一定会被执行,即使在标识为@BeforeClass的方法抛出异常的的情况下也一样会。所有标识为@After 的方法都一定会被执行,即使在标识为 @Before 或者 @Test 的方法抛出异常的的情况下也一样会。

 

@BeforeClass 和 @AfterClass 对于那些比较“昂贵”的资源的分配或者释放来说是很有效的,因为他们只会在类中被执行一次。相比之下对于那些需要在每次运行之前都要初始化或者在运行之后 都需要被清理的资源来说使用@Before和@After同样是一个比较明智的选择。



  • 10
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Java,@Before、@Test和@After是JUnit测试框架注解,用于定义测试方法执行前、测试方法执行测试方法执行后的操作。 举个例子,假设我们要测试一个Calculator类的add()方法,我们可以定义一个测试类CalculatorTest,并在其使用@Before、@Test和@After注解来编写测试方法。 ```java import org.junit.After; import org.junit.Before; import org.junit.Test; import static org.junit.Assert.assertEquals; public class CalculatorTest { private Calculator calculator; @Before public void setUp() { calculator = new Calculator(); } @Test public void testAdd() { int result = calculator.add(2, 3); assertEquals(5, result); } @After public void tearDown() { calculator = null; } } ``` 在上面的示例,@Before注解指定了setUp()方法,在每个测试方法执行前都会执行它。在setUp()方法,我们创建了一个Calculator对象,以便在测试方法使用它。 @Test注解用于标记testAdd()方法,它是我们要测试的add()方法。在testAdd()方法,我们调用了calculator.add(2, 3)方法,并验证了结果是否等于5。 @After注解指定了tearDown()方法,在每个测试方法执行后都会执行它。在tearDown()方法,我们将calculator对象设置为null,以便在下一个测试方法创建一个新的Calculator对象。 通过使用@Before、@Test和@After注解,我们可以轻松地编写单元测试,并确保每个测试方法都在一个干净的环境执行。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值