Answers:
标记的代码@Before
在每次测试之前执行,而@BeforeClass
在整个测试夹具之前运行一次。如果您的测试类有十个测试,则@Before
代码将执行十次,但@BeforeClass
仅执行一次。
通常,@BeforeClass
当多个测试需要共享相同的计算昂贵的设置代码时,可以使用。建立数据库连接属于此类。您可以将代码从@BeforeClass
移到@Before
,但测试运行可能需要更长的时间。注意,标记的代码@BeforeClass
作为静态初始化程序运行,因此它将在创建测试夹具的类实例之前运行。
在JUnit 5中,标记@BeforeEach
和与JUnit 4中@BeforeAll
的@Before
和等效。@BeforeClass
它们的名称更能指示它们的运行时间,松散地解释为:“在每个测试之前”和“一次在所有测试之前”。
@BeforeClas
是静态的。它在创建测试类实例之前运行。
@BeforeAll
可能是非静态的,并在每次运行新的测试实例时调用它。请参阅相应的答案stackoverflow.com/a/55720750/1477873
每个注释之间的区别是:
+-------------------------------------------------------------------------------------------------------+
¦ 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
函数@Before
注释将在具有@Test
注释的类中的每个测试函数之前执行,但是with函数@BeforeClass
将仅在类中的所有测试函数之前执行一次。
类似地,带有@After
注解的函数将在类中具有@Test
注解的每个测试函数之后执行,但是带注解的函数@AfterClass
将在类中的所有测试函数之后仅执行一次。
样本类
public class SampleClass {
public String initializeData(){
return "Initialize";
}
public String processDate(){
return "Process";
}
}
样品测试
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
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())
}
}