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

本文翻译自:Difference between @Before, @BeforeClass, @BeforeEach and @BeforeAll

What is the main difference between 之间的主要区别是什么

  • @Before and @BeforeClass @Before@BeforeClass
    • and in JUnit 5 @BeforeEach and @BeforeAll 在JUnit 5中@BeforeEach@BeforeAll
  • @After and @AfterClass @After@AfterClass

According to the JUnit Api @Before is used in the following case: 根据JUnit Api ,在以下情况下使用@Before

When writing tests, it is common to find that several tests need similar objects created before they can run. 编写测试时,通常会发现几个测试需要先创建类似的对象,然后才能运行。

Whereas @BeforeClass can be used to establish a database connection. @BeforeClass可用于建立数据库连接。 But couldn't @Before do the same? 但是@Before不能一样吗?


#1楼

参考:https://stackoom.com/question/1N9o2/Before-BeforeClass-BeforeEach和-BeforeAll之间的区别


#2楼

The code marked @Before is executed before each test, while @BeforeClass runs once before the entire test fixture. 标有@Before的代码在每次测试之前执行,而@BeforeClass在整个测试夹具之前运行一次。 If your test class has ten tests, @Before code will be executed ten times, but @BeforeClass will be executed only once. 如果您的测试类有十个测试,则@Before代码将执行十次,但是@BeforeClass将仅执行一次。

In general, you use @BeforeClass when multiple tests need to share the same computationally expensive setup code. 通常,当多个测试需要共享相同的计算昂贵的设置代码时,可以使用@BeforeClass Establishing a database connection falls into this category. 建立数据库连接属于此类。 You can move code from @BeforeClass into @Before , but your test run may take longer. 您可以将代码从@BeforeClass移到@Before ,但是您的测试运行可能需要更长的时间。 Note that the code marked @BeforeClass is run as static initializer, therefore it will run before the class instance of your test fixture is created. 注意,标记为@BeforeClass的代码作为静态初始化程序运行,因此它将在创建测试夹具的类实例之前运行。

In JUnit 5 , the tags @BeforeEach and @BeforeAll are the equivalents of @Before and @BeforeClass in JUnit 4. Their names are a bit more indicative of when they run, loosely interpreted: 'before each tests' and 'once before all tests'. JUnit 5中 ,标签@BeforeEach@BeforeAll与JUnit 4中的@Before@BeforeClass等效。它们的名称更能指示它们的运行时间,松散地解释为:“每次测试之前”和“一次在所有测试之前”。 '。


#3楼

Difference between each annotation are : 每个注释之间的区别是:

+-------------------------------------------------------------------------------------------------------+
¦                                       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.                        ¦              ¦             ¦
+-------------------------------------------------------------------------------------------------------+

Most of annotations in both versions are same, but few differs. 两个版本中的大多数注释都相同,但几乎没有区别。

Reference 参考

Order of Execution. 执行顺序。

Dashed box -> optional annotation. 虚线框->可选注释。

在此处输入图片说明


#4楼

Before and BeforeClass in JUnit JUnit中的BeforeClass和BeforeClass

The function @Before annotation will be executed before each of test function in the class having @Test annotation but the function with @BeforeClass will be execute only one time before all the test functions in the class. @Before注释函数将在具有@Test注释的类中的每个测试函数之前执行,而具有@BeforeClass的函数仅在类中的所有测试函数之前执行一次。

Similarly function with @After annotation will be executed after each of test function in the class having @Test annotation but the function with @AfterClass will be execute only one time after all the test functions in the class. 类似地,具有@After批注的函数将在类中具有@Test批注的每个测试函数之后执行,但是具有@AfterClass的函数仅在该类中的所有测试函数之后执行一次。

SampleClass 样本类

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

    public String processDate(){
        return "Process";
    }
 }

SampleTest 样品测试

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() );
    }

}

Output 输出量

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

In Junit 5 在Junit 5中

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

#5楼

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())
    }
}

Same as 如同

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())
    }
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值