![junit5](https://i-blog.csdnimg.cn/blog_migrate/3286bdba6fa97291873839d10453cbe9.png)
junit5
JUnit5最终版本即将来临(当前是M4),我已经开始研究如何编写扩展。
在JUnit5中,您没有使用Runners , Rules , ClassRules等,而是只有一个Extension API来实现自己的扩展。
JUnit5提供了多个接口来挂钩其生命周期。 例如,您可以挂钩到“测试实例后处理”以在测试实例上调用自定义初始化方法,或者通过“参数解析”来在运行时动态解析测试方法参数。 当然,到目前为止,典型的方法例如在执行所有测试之前,执行测试之前,执行测试之后进行钩子等等,可以在http://junit.org/junit5/docs/找到完整列表。当前/用户指南/#extensions-lifecycle-callbacks
但是,在每个过程中都在过程的哪一点执行? 为了测试它,我刚刚创建了一个扩展,该扩展实现了所有接口,并且每个方法都会打印出它是谁。
public class LoggerExtension implements TestInstancePostProcessor, ParameterResolver, BeforeAllCallback,
BeforeEachCallback, BeforeTestExecutionCallback, AfterEachCallback, AfterTestExecutionCallback, AfterAllCallback,
TestExecutionExceptionHandler {
@Override
public void postProcessTestInstance(Object testInstance, ExtensionContext context) throws Exception {
System.out.println("Test Instance Post-processing called");
}
@Override
public boolean supports(ParameterContext parameterContext, ExtensionContext extensionContext)
throws ParameterResolutionException {
System.out.println("Parameter Resolver Supports called");
return parameterContext.getParameter().getType().equals(String.class);
}
@Override
public Object resolve(ParameterContext parameterContext, ExtensionContext extensionContext)
throws ParameterResolutionException {
System.out.println("Resolver called");
return "Hello World";
}
@Override
public void beforeAll(ContainerExtensionContext context) throws Exception {
System.out.println("Before All called " + context.getTestClass().get());
}
@Override
public void beforeEach(TestExtensionContext context) throws Exception {
System.out.println("Before Each called");
}
@Override
public void beforeTestExecution(TestExtensionContext context) throws Exception {
System.out.println("Before Test Execution called");
}
@Override
public void afterEach(TestExtensionContext context) throws Exception {
System.out.println("After Each called");
}
@Override
public void afterTestExecution(TestExtensionContext context) throws Exception {
System.out.println("After Test Executon called");
}
@Override
public void afterAll(ContainerExtensionContext context) throws Exception {
System.out.println("After All called");
}
@Override
public void handleTestExecutionException(TestExtensionContext context, Throwable throwable) throws Throwable {
System.out.println("Test Execution Exception called");
throw throwable;
}
}
然后,我创建了一个包含两个测试的JUnit5测试套件:
@ExtendWith(LoggerExtension.class)
public class AnotherLoggerExtensionTest {
@Test
public void test4() {
System.out.println("Test 4");
}
}
@ExtendWith(LoggerExtension.class)
public class LoggerExtensionTest {
@Test
public void test1() {
System.out.println("Test 1");
}
@Test
public void test2(String msg) {
System.out.println("Test 2 " + msg);
}
@Test
public void test3() {
System.out.println("Test 3");
throw new IllegalArgumentException("");
}
}
@RunWith(JUnitPlatform.class)
@SelectClasses({LoggerExtensionTest.class, AnotherLoggerExtensionTest.class})
public class LoggerExtensionTestSuite {
}
那么在执行此套件之后,输出是什么? 让我们来看看它。 请注意,出于可读性考虑,我在终端输出上添加了一些标注。
Before All called class AnotherLoggerExtensionTest
Test Instance Post-processing called
Before Each called
Before Test Execution called
Test 4
After Test Execution called
After Each called
After All called
// <1>
Before All called class LoggerExtensionTest
Test Instance Post-processing called
Before Each called
Before Test Execution called
Test 1
After Test Execution called
After Each called
// <2>
Test Instance Post-processing called
Before Each called
Before Test Execution called
Parameter Resolver Supports called
Resolver called
Test 2 Hello World
After Test Execution called
After Each called
// <3>
Test Instance Post-processing called
Before Each called
Before Test Execution called
Test 3
Test Execution Exception called
After Test Execution called
After Each called
// <4>
After All called
<1>运行的第一个测试是AnotherLoggerExtensionTest 。 在这种情况下,只有一个简单的测试,因此扩展的生命周期为BeforeAll , Test Instance-Post-Processing , Before Each , Before Test Execution ,然后执行测试本身,然后执行所有After回调。
<2>然后执行LoggerExtensionTest 。 第一次测试不是参数化测试,因此不会调用与参数解析有关的事件。 在执行test方法之前,将调用测试实例后处理,然后再引发所有事件之前。 最后,对所有事件进行测试。
<3>第二个测试包含需要参数解析的内容。 参数解析器在Before事件之后和执行测试本身之前运行。
<4>最后一次测试将引发异常。 在执行测试之后但在After事件之前调用Test Execution Exception 。
最后要注意的是, BeforeAll和AfterAll事件是按测试类而不是套件执行的。
本示例中使用的JUnit版本是org.junit.jupiter:junit-jupiter-api:5.0.0-M4
翻译自: https://www.javacodegeeks.com/2017/06/lifecycle-junit-5-extension-model.html
junit5