use*_*330 404 java junit annotations junit4 junit5
两者之间的主要区别是什么
@Before
和 @BeforeClass
@BeforeEach
和@BeforeAll
@After
和 @AfterClass
根据JUnit Api @Before
用于以下情况:
编写测试时,通常会发现多个测试需要在运行之前创建类似的对象.
而@BeforeClass
可用于建立数据库连接.但不能@Before
做同样的事情?
das*_*ght 591
标记的代码@Before
在每次测试之前执行,而@BeforeClass
在整个测试夹具之前运行一次.如果您的测试类有十个测试,@Before
代码将执行十次,但@BeforeClass
只执行一次.
通常,@BeforeClass
当多个测试需要共享相同的计算昂贵的设置代码时,您使用.建立数据库连接属于此类别.您可以将代码@BeforeClass
移入@Before
,但您的测试运行可能需要更长时间.请注意,标记的代码@BeforeClass
作为静态初始化程序运行,因此它将在创建测试夹具的类实例之前运行.
在JUnit的5,标签@BeforeEach
和@BeforeAll
是等价@Before
和@BeforeClass
JUnit 4中他们的名字是多一点表明它们在运行时的,松散的解释:"以前每次测试"和"一旦所有测试之前".
Job*_*ews 113
每个注释之间的区别是:
+-------------------------------------------------------------------------------------------------------+
¦ 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. ¦ ¦ ¦
+-------------------------------------------------------------------------------------------------------+
Run Code Online (Sandbox Code Playgroud)
两个版本中的大多数注释都是相同的,但很少有区别.
执行顺序.
虚线框 - >可选注释.
yoA*_*ex5 12
JUnit @BeforeEach、@AfterEach、@BeforeAll、@AfterAll
@Before
(JUnit4) -> (JUnit5) - 在每次测试之前@BeforeEach
调用方法
@After
(JUnit4) -> (JUnit5) -每次测试后@AfterEach
调用方法
@BeforeClass
(JUnit4) -> @BeforeAll
(JUnit5) -在执行此类中的所有测试之前调用静态方法。这可能是一项艰巨的任务,如启动服务器、读取文件、建立数据库连接......
@AfterClass
(JUnit4) -> @AfterAll
(JUnit5) -在执行此类中的所有测试后调用静态方法。
JUnit中的BeforeClass和BeforeClass
函数@Before
注释将在具有@Test
注释的类中的每个测试函数之前执行,但是with函数@BeforeClass
将仅在类中的所有测试函数之前执行一次。
类似地,带有@After
注解的函数将在类中具有@Test
注解的每个测试函数之后执行,但是带注解的函数@AfterClass
将在类中的所有测试函数之后仅执行一次。
样本类
public class SampleClass {
public String initializeData(){
return "Initialize";
}
public String processDate(){
return "Process";
}
}
Run Code Online (Sandbox Code Playgroud)
样品测试
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() );
}
}
Run Code Online (Sandbox Code Playgroud)
输出量
Before Class
Before Function
After Function
Before Function
After Function
After Class
Run Code Online (Sandbox Code Playgroud)
在Junit 5中
@Before = @BeforeEach
@BeforeClass = @BeforeAll
@After = @AfterEach
@AfterClass = @AfterAll
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
253680 次 |
最近记录: |