Difference between @Before, @BeforeClass, @BeforeEach and @BeforeAll
之间的主要区别是什么
-
@Before 和@BeforeClass -
在JUnit 5
@BeforeEach 和@BeforeAll 中
-
在JUnit 5
-
@After 和@AfterClass
根据JUnit,在以下情况下使用Api
When writing tests, it is common to find that several tests need similar objects created before they can run.
而
标记为
通常,当多个测试需要共享相同的计算昂贵的设置代码时,请使用
在JUnit 5中,标记
每个注释之间的区别是:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | +-------------------------------------------------------------------------------------------------------+ | 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. | | | +-------------------------------------------------------------------------------------------------------+ |
两个版本中的大多数注释都相同,但几乎没有区别。
参考
执行顺序。
虚线框->可选注释。
JUnit中的BeforeClass和BeforeClass
函数
类似地,具有
SampleClass
1 2 3 4 5 6 7 8 9 |
SampleTest
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | 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() ); } } |
产量
1 2 3 4 5 6 | Before Class Before Function After Function Before Function After Function After Class |
在Junit 5中
1 2 3 4 | @Before = @BeforeEach @BeforeClass = @BeforeAll @After = @AfterEach @AfterClass = @AfterAll |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | 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()) } } |
如同
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | 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()) } } |