深圳网站制作的公司网络服务,免费的网站建造,扁平式的网站,商丘优化公司扩展 junit 框架JUnit5最终版本即将来临 #xff08;当前是M4#xff09;#xff0c;我已经开始尝试如何编写扩展了。 在JUnit5中 #xff0c;您没有使用Runners #xff0c; Rules #xff0c; ClassRules等#xff0c;而是只有一个Extension API来实现自己的扩展。 … 扩展 junit 框架 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 {Overridepublic void postProcessTestInstance(Object testInstance, ExtensionContext context) throws Exception {System.out.println(Test Instance Post-processing called);}Overridepublic boolean supports(ParameterContext parameterContext, ExtensionContext extensionContext)throws ParameterResolutionException {System.out.println(Parameter Resolver Supports called);return parameterContext.getParameter().getType().equals(String.class);}Overridepublic Object resolve(ParameterContext parameterContext, ExtensionContext extensionContext)throws ParameterResolutionException {System.out.println(Resolver called);return Hello World;}Overridepublic void beforeAll(ContainerExtensionContext context) throws Exception {System.out.println(Before All called context.getTestClass().get());}Overridepublic void beforeEach(TestExtensionContext context) throws Exception {System.out.println(Before Each called);}Overridepublic void beforeTestExecution(TestExtensionContext context) throws Exception {System.out.println(Before Test Execution called);}Overridepublic void afterEach(TestExtensionContext context) throws Exception {System.out.println(After Each called);}Overridepublic void afterTestExecution(TestExtensionContext context) throws Exception {System.out.println(After Test Executon called);}Overridepublic void afterAll(ContainerExtensionContext context) throws Exception {System.out.println(After All called);}Overridepublic void handleTestExecutionException(TestExtensionContext context, Throwable throwable) throws Throwable {System.out.println(Test Execution Exception called);throw throwable;}
} 然后我创建了一个包含两个测试的JUnit5测试套件 ExtendWith(LoggerExtension.class)
public class AnotherLoggerExtensionTest {Testpublic void test4() {System.out.println(Test 4);}}ExtendWith(LoggerExtension.class)
public class LoggerExtensionTest {Testpublic void test1() {System.out.println(Test 1);}Testpublic void test2(String msg) {System.out.println(Test 2 msg);}Testpublic 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// 1Before 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// 2Test 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// 3Test Instance Post-processing called
Before Each called
Before Test Execution called
Test 3
Test Execution Exception called
After Test Execution called
After Each called// 4After 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.jupiterjunit-jupiter-api5.0.0-M4 翻译自: https://www.javacodegeeks.com/2017/06/lifecycle-junit-5-extension-model.html扩展 junit 框架