@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())}
}