之间的主要区别是什么
@Before 和 @BeforeClass 以及 JUnit 5 中的 @BeforeEach 和 @BeforeAll
在 JUnit 5 中 @BeforeEach 和 @BeforeAll
@After 和 @AfterClass
根据JUnit Api @Before
用于以下情况:
在编写测试时,通常会发现多个测试需要创建类似的对象才能运行。
而 @BeforeClass
可用于建立数据库连接。但是 @Before
不能做同样的事情吗?
标记为 @Before
的代码在每次测试之前执行,而 @BeforeClass
在整个测试夹具之前运行一次。如果您的测试类有十个测试,则 @Before
代码将执行十次,但 @BeforeClass
将只执行一次。
通常,当多个测试需要共享相同的计算量大的设置代码时,您可以使用 @BeforeClass
。建立数据库连接属于这一类。您可以将代码从 @BeforeClass
移到 @Before
,但您的测试运行可能需要更长时间。请注意,标记为 @BeforeClass
的代码作为静态初始化程序运行,因此它将在创建测试夹具的类实例之前运行。
在 JUnit 5 中,标签 @BeforeEach
和 @BeforeAll
与 JUnit 4 中的 @Before
和 @BeforeClass
等效。它们的名称更能说明它们的运行时间,松散地解释为:“在每个测试之前”和'在所有测试之前'。
每个注释之间的区别是:
+-------------------------------------------------------------------------------------------------------+
¦ 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. ¦ ¦ ¦
+-------------------------------------------------------------------------------------------------------+
两个版本中的大多数注释都是相同的,但很少有不同。
执行顺序。
虚线框 -> 可选注释。
https://i.stack.imgur.com/HKspz.png
JUnit 中的 Before 和 BeforeClass
函数 @Before
注释将在具有 @Test
注释的类中的每个测试函数之前执行,但具有 @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
六月 5
@Before = @BeforeEach
@BeforeClass = @BeforeAll
@After = @AfterEach
@AfterClass = @AfterAll
@Before
(JUnit4) -> @BeforeEach
(JUnit5) - 每次测试之前调用方法
@After
(JUnit4) -> @AfterEach
(JUnit5) - 每次测试在之后调用方法
@BeforeClass
(JUnit4) -> @BeforeAll
(JUnit5) - static 方法在执行此类中的所有测试之前调用 。启动服务器、读取文件、建立数据库连接可能是一项艰巨的任务......
@AfterClass
(JUnit4) -> @AfterAll
(JUnit5) - 静态 方法被调用 执行该类中的所有测试。
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())
}
}
所有这些注释之间的基本区别如下 -
@BeforeEach - 用于在(例如 setUp)每个测试方法执行之前运行公共代码。类似于 JUnit 4 的 @Before。 @AfterEach - 用于在(例如,tearDown)每个测试方法执行之后运行公共代码。类似于 JUnit 4 的 @After。 @BeforeAll - 用于在任何测试执行之前为每个类运行一次。类似于 JUnit 4 的 @BeforeClass。 @AfterAll - 用于在所有测试执行后每个类运行一次。类似于 JUnit 4 的 @AfterClass。
所有这些注释以及用法都在 Codingeek - Junit5 Test Lifecycle 上定义
@BeforeClass
将在整个测试套件之前运行,而 @Before
将在每个测试之前执行,而 @BeforeClass
在整个测试夹具之前运行一次。如果您的测试类有十个测试,@Before
代码将执行十次,但 @BeforeClass
将只执行一次。
不定期副业成功案例分享
@BeforeClas
是静态的。它在创建测试类实例之前运行。@BeforeAll
可能是非静态的,并且会在每个新的测试实例运行时调用。查看对应答案stackoverflow.com/a/55720750/1477873Before
和BeforeEach
是同一件事吗,因为当我在init()
函数上使用@Before
但与@BeforeEach
一起使用时,我的测试用例未能通过模拟