@Before,@BeforeClass,@BeforeEach和@BeforeAll之间的区别

本文详细介绍了JUnit4和JUnit5中的关键测试注解,包括@BeforeClass/@BeforeAll、@Before/@BeforeEach、@AfterClass/@AfterAll及@After/@AfterEach的区别与用法,并通过示例代码展示了这些注解如何帮助组织和管理测试流程。

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

@Before的代码在每次测试之前执行

@BeforeClass在整个测试方法执行之前运行一次

如果您的测试类有十个测试,则@Before代码将执行十次,但是@BeforeClass将仅执行一次。

当多个测试需要共享相同的代码时,可以使用@BeforeClass 建立数据库连接属于此类。

您可以将代码从@BeforeClass移到@Before ,但是您的测试运行可能需要更长的时间。

注意,标记为@BeforeClass的代码作为静态初始化程序运行,因此它将在创建测试夹具的类实例之前运行。

JUnit 5中 ,标签@BeforeEach@BeforeAll与JUnit 4中的@Before@BeforeClass等效。

它们的名称更能说明它们的运行时间,可以解释为:“在所有测试之前执行一次”和每次测试之前执行。 

Junit4和Junit5中每个注释之间的区别:

+-------------------------------------------------------------------------------------------------------+
¦                                       Feature                            ¦   Junit 4    ¦   Junit 5   ¦
¦--------------------------------------------------------------------------+--------------+-------------¦
¦ Execute before all test methods of the class are executed.               ¦ @BeforeClass ¦ @BeforeAll  ¦
¦ Used with static method.                                                 ¦              ¦             ¦
¦ For example, This method could contain some initialization code          ¦              ¦             ¦
¦-------------------------------------------------------------------------------------------------------¦
¦ Execute after all test methods in the current class.                     ¦ @AfterClass  ¦ @AfterAll   ¦
¦ Used with static method.                                                 ¦              ¦             ¦
¦ For example, This method could contain some cleanup code.                ¦              ¦             ¦
¦-------------------------------------------------------------------------------------------------------¦
¦ Execute before each test method.                                         ¦ @Before      ¦ @BeforeEach ¦
¦ Used with non-static method.                                             ¦              ¦             ¦
¦ For example, to reinitialize some class attributes used by the methods.  ¦              ¦             ¦
¦-------------------------------------------------------------------------------------------------------¦
¦ Execute after each test method.                                          ¦ @After       ¦ @AfterEach  ¦
¦ Used with non-static method.                                             ¦              ¦             ¦
¦ For example, to roll back database modifications.                        ¦              ¦             ¦
+-------------------------------------------------------------------------------------------------------+

 两个版本中的大多数注释都相同,但几乎没有区别

执行顺序

 

@Before注释函数将在具有@Test注释的类中的每个测试函数之前执行

@BeforeClass注释函数仅在类中的所有测试函数之前执行一次

@After注释函数将在类中具有@Test批注的每个测试函数之后执行

@AfterClass注释函数仅在该类中的所有测试函数之后执行一次

public class SampleClass {
    public String initializeData(){
        return "Initialize";
    }
 
    public String processDate(){
        return "Process";
    }
 }

测试

public class SampleTest {
 
    private SampleClass sampleClass;
 
    @BeforeClass
    public static void beforeClassFunction(){
        System.out.println("Before Class");
    }
 
    @Before
    public void beforeFunction(){
        sampleClass=new SampleClass();
        System.out.println("Before Function");
    }
 
    @After
    public void afterFunction(){
        System.out.println("After Function");
    }
 
    @AfterClass
    public static void afterClassFunction(){
        System.out.println("After Class");
    }
 
    @Test
    public void initializeTest(){
        Assert.assertEquals("Initailization check", "Initialize", sampleClass.initializeData() );
    }
 
    @Test
    public void processTest(){
        Assert.assertEquals("Process check", "Process", sampleClass.processDate() );
    }
 
}

输出

Before Class
Before Function
After Function
Before Function
After Function
After Class

Junit 5中

@Before = @BeforeEach
@BeforeClass = @BeforeAll
@After = @AfterEach
@AfterClass = @AfterAll

 

import org.junit.Assert
import org.junit.Before
import org.junit.BeforeClass
import org.junit.Test
 
class FeatureTest {
    companion object {
        private lateinit var heavyFeature: HeavyFeature
        @BeforeClass
        @JvmStatic
        fun beforeHeavy() {
            heavyFeature = HeavyFeature()
        }
    }
 
    private lateinit var feature: Feature
 
    @Before
    fun before() {
        feature = Feature()
    }
 
    @Test
    fun testCool() {
        Assert.assertTrue(heavyFeature.cool())
        Assert.assertTrue(feature.cool())
    }
 
    @Test
    fun testWow() {
        Assert.assertTrue(heavyFeature.wow())
        Assert.assertTrue(feature.wow())
    }
}

如同

import org.junit.Assert
import org.junit.Test
 
 class FeatureTest {
    companion object {
        private val heavyFeature = HeavyFeature()
    }
 
    private val feature = Feature()
 
    @Test
    fun testCool() {
        Assert.assertTrue(heavyFeature.cool())
        Assert.assertTrue(feature.cool())
    }
 
    @Test
    fun testWow() {
        Assert.assertTrue(heavyFeature.wow())
        Assert.assertTrue(feature.wow())
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JackieZhengChina

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值