@Before
的代碼在每次測試之前執行
@BeforeClass
在整個測試方法執行之前運行一次
如果您的測試類有十個測試,則@Before
代碼將執行十次,但是@BeforeClass
將僅執行一次。
當多個測試需要共享相同的代碼時,可以使用@BeforeClass
。 建立數據庫連接屬于此類。
您可以將代碼從@BeforeClass
移到@Before
,但是您的測試運行可能需要更長的時間。
注意,標記為@BeforeClass
的代碼作為靜態初始化程序運行,因此它將在創建測試夾具的類實例之前運行。
在JUnit 5中 ,標簽@BeforeEach
和@BeforeAll
與JUnit 4中的@Before
和@BeforeClass
等效。
它們的名稱更能說明它們的運行時間,可以解釋為:“在所有測試之前執行一次”和“每次測試之前執行”。?
Junit4和Junit5中每個注釋之間的區別:
+-------------------------------------------------------------------------------------------------------+
| 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. | | |
+-------------------------------------------------------------------------------------------------------+
?兩個版本中的大多數注釋都相同,但幾乎沒有區別
執行順序
?
@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;@BeforeClasspublic static void beforeClassFunction(){System.out.println("Before Class");}@Beforepublic void beforeFunction(){sampleClass=new SampleClass();System.out.println("Before Function");}@Afterpublic void afterFunction(){System.out.println("After Function");}@AfterClasspublic static void afterClassFunction(){System.out.println("After Class");}@Testpublic void initializeTest(){Assert.assertEquals("Initailization check", "Initialize", sampleClass.initializeData() );}@Testpublic 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.Testclass FeatureTest {companion object {private lateinit var heavyFeature: HeavyFeature@BeforeClass@JvmStaticfun beforeHeavy() {heavyFeature = HeavyFeature()}}private lateinit var feature: Feature@Beforefun before() {feature = Feature()}@Testfun testCool() {Assert.assertTrue(heavyFeature.cool())Assert.assertTrue(feature.cool())}@Testfun testWow() {Assert.assertTrue(heavyFeature.wow())Assert.assertTrue(feature.wow())}
}
如同
import org.junit.Assert
import org.junit.Testclass FeatureTest {companion object {private val heavyFeature = HeavyFeature()}private val feature = Feature()@Testfun testCool() {Assert.assertTrue(heavyFeature.cool())Assert.assertTrue(feature.cool())}@Testfun testWow() {Assert.assertTrue(heavyFeature.wow())Assert.assertTrue(feature.wow())}
}