java - (09) JUnit4中@AfterClass @BeforeClass @after @before的区别对比

本文详细介绍了JUnit4的基础知识,包括常用注解的使用方法及其执行顺序。通过具体示例代码展示了@BeforeClass、@AfterClass、@Before和@After等注解的应用场景及区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.基础知识

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;

 

二.实例

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

结果:

Text代码  收藏代码
  1. @BeforeClass  
  2. @Before  
  3. @Test(timeout = 50)  
  4. @After  
  5. @Before  
  6. @Test(expected = Exception.class)  
  7. @After  
  8. @Before  
  9. @Test  
  10. @After  
  11. @AfterClass  

 

三.@BeforeClass、@AfterClass与@Before、@After的对比:

@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 的方法抛出异常的的情况下也一样会。

        特别说明:

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

        2.如果类里面可以有多个注解过@Before和@After的方法,它们的执行顺序是未知的;

        3.@BeforeClass  @AfterClass注解是junit提供的另外的两个注解,必须设置在public 静态方法之上,表示在class加载之前执行,这样设置的方法只会执行一次,而@Before @After则会再每次test之前/之后执行;

        4.在JUnit4中,如果测试类继承了TestCase类,那么所有的Annotation都不会起作用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值