本文翻译自: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
- and in JUnit 5
-
@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. 两个版本中的大多数注释都相同,但几乎没有区别。
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())
}
}