– Start
默认情况下,运行每个测试用例前都会实例化它所在的类,这就是 @BeforeAll 和 @AfterAll 需要是静态的原因。不过我们也可以设置成只实例化一次。
package demo13;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;
// 只实例化一次,然后运行所有测试用例
@TestInstance(Lifecycle.PER_CLASS)
class TestInstanceTest {
// 注意此方法不是静态的
@BeforeAll
void setUpBeforeClass() throws Exception {
System.out.println("@BeforeAll -- 在所有测试方法之前运行一次");
}
// 注意此方法不是静态的
@AfterAll
void tearDownAfterClass() throws Exception {
System.out.println("@AfterAll -- 在所有测试方法之后运行一次");
}
@BeforeEach
void setUp() throws Exception {
System.out.println(" @BeforeEach -- 在每个测试方法之前运行一次");
}
@AfterEach
void tearDown() throws Exception {