Java @Before、@BeforeClass、@BeforeEach 和 @BeforeAll 之间的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20295578/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between @Before, @BeforeClass, @BeforeEach and @BeforeAll
提问by user1170330
What is the main difference between
之间的主要区别是什么
@Before
and@BeforeClass
- and in JUnit 5
@BeforeEach
and@BeforeAll
- and in JUnit 5
@After
and@AfterClass
@Before
和@BeforeClass
- 在 JUnit 5
@BeforeEach
和@BeforeAll
- 在 JUnit 5
@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. But couldn't @Before
do the same?
而@BeforeClass
可用于建立数据库连接。但不能@Before
这样做吗?
采纳答案by dasblinkenlight
The code marked @Before
is executed before each test, while @BeforeClass
runs once before the entire test fixture. If your test class has ten tests, @Before
code will be executed ten times, but @BeforeClass
will be executed only once.
标记的代码@Before
在每次测试之前执行,而@BeforeClass
在整个测试装置之前运行一次。如果您的测试类有 10 个测试,则@Before
代码将执行 10 次,但@BeforeClass
只会执行一次。
In general, you use @BeforeClass
when multiple tests need to share the same computationally expensive setup code. Establishing a database connection falls into this category. You can move code from @BeforeClass
into @Before
, but your test run may take longer. 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
当多个测试需要共享相同的计算成本高的设置代码时使用。建立数据库连接就属于这一类。您可以将代码从 移动@BeforeClass
到@Before
,但您的测试运行可能需要更长的时间。请注意,标记的代码@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
它们的名称更能说明它们何时运行,松散地解释为:“每次测试之前”和“所有测试之前一次”。
回答by Joby Wilson Mathews
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.
虚线框 -> 可选注释。
回答by Dhyan Mohandas
Before and BeforeClass in JUnit
JUnit 中的 Before 和 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
回答by kreker
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())
}
}