主要的区别是什么
@Before和@BeforeClass 在JUnit 5中@BeforeEach和@BeforeAll @课后和@课后
根据JUnit Api, @Before用于以下情况:
在编写测试时,经常会发现几个测试在运行之前需要创建类似的对象。
而@BeforeClass可以用来建立数据库连接。但是@Before不能做同样的事情吗?
主要的区别是什么
@Before和@BeforeClass 在JUnit 5中@BeforeEach和@BeforeAll @课后和@课后
根据JUnit Api, @Before用于以下情况:
在编写测试时,经常会发现几个测试在运行之前需要创建类似的对象。
而@BeforeClass可以用来建立数据库连接。但是@Before不能做同样的事情吗?
当前回答
每个注释之间的区别是:
+-------------------------------------------------------------------------------------------------------+
¦ 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中的Before和BeforeClass
函数@Before注释将在具有@Test注释的类中的每个测试函数之前执行,但是具有@BeforeClass注释的函数将只在类中的所有测试函数之前执行一次。
类似地,带有@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() );
}
}
输出
Before Class
Before Function
After Function
Before Function
After Function
After Class
在Junit 5中
@Before = @BeforeEach
@BeforeClass = @BeforeAll
@After = @AfterEach
@AfterClass = @AfterAll
每个注释之间的区别是:
+-------------------------------------------------------------------------------------------------------+
¦ 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. ¦ ¦ ¦
+-------------------------------------------------------------------------------------------------------+
两个版本中的大多数注释是相同的,但很少不同。
参考
执行顺序。
虚线框->可选注释。
@BeforeClass将在整个测试套件之前运行,而@Before将在每个测试之前运行,而@BeforeClass将在整个测试fixture之前运行一次。如果测试类有10个测试,@Before代码将执行10次,但@BeforeClass将只执行一次。
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())
}
}
标记为@Before的代码在每次测试之前执行,而@BeforeClass在整个测试装置之前运行一次。如果测试类有10个测试,@Before代码将执行10次,但@BeforeClass将只执行一次。
一般来说,当多个测试需要共享相同的计算成本高昂的设置代码时,您可以使用@BeforeClass。建立数据库连接就属于这一类。您可以将代码从@BeforeClass移到@Before,但测试运行可能会花费更长的时间。注意,标记为@BeforeClass的代码是作为静态初始化器运行的,因此它将在创建测试fixture的类实例之前运行。
在JUnit 5中,标签@BeforeEach和@BeforeAll相当于JUnit 4中的@Before和@BeforeClass。它们的名称更能说明它们的运行时间,可以粗略地解释为:“在每次测试之前”和“在所有测试之前一次”。