JUnit 5扩展模型的生命周期
JUnit5最終版本即將來臨 (當(dāng)前是M4),我已經(jīng)開始研究如何編寫擴展。
在JUnit5中 ,您沒有使用Runners , Rules , ClassRules等,而是只有一個Extension API來實現(xiàn)自己的擴展。
JUnit5提供了多個接口來掛鉤其生命周期。 例如,您可以掛鉤到“ 測試實例后處理”以在測試實例上調(diào)用自定義初始化方法,或者通過“ 參數(shù)解析”來在運行時動態(tài)解析測試方法參數(shù)。 當(dāng)然,到目前為止,典型的操作如在執(zhí)行所有測試之前,執(zhí)行測試之前,執(zhí)行測試之后進行掛接等等,可以在http://junit.org/junit5/docs/找到完整列表。 當(dāng)前/用戶指南/#extensions-lifecycle-callbacks
但是在每個過程中都在過程的哪一點執(zhí)行? 為了測試它,我剛剛創(chuàng)建了一個擴展,該擴展實現(xiàn)了所有接口,并且每個方法都會打印出誰。
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;} }然后,我創(chuàng)建了一個包含兩個測試的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 { }那么在執(zhí)行此套件之后,輸出是什么? 讓我們來看看它。 請注意,出于可讀性考慮,我在終端輸出上添加了一些標(biāo)注。
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 ,然后執(zhí)行測試本身,然后執(zhí)行所有After回調(diào)。
<2>然后執(zhí)行LoggerExtensionTest 。 第一次測試不是參數(shù)化測試,因此不會調(diào)用與參數(shù)解析有關(guān)的事件。 在執(zhí)行test方法之前,將調(diào)用測試實例后處理 ,然后再引發(fā)所有事件之前。 最終,所有后續(xù)事件都將執(zhí)行測試。
<3>第二個測試包含需要參數(shù)解析。 參數(shù)解析器在Before事件之后和執(zhí)行測試本身之前運行。
<4>最后一次測試將引發(fā)異常。 在執(zhí)行測試之后但在After事件之前調(diào)用Test Execution Exception 。
最后要注意的是, BeforeAll和AfterAll事件是按測試類而不是套件執(zhí)行的。
本示例中使用的JUnit版本是org.junit.jupiter:junit-jupiter-api:5.0.0-M4
翻譯自: https://www.javacodegeeks.com/2017/06/lifecycle-junit-5-extension-model.html
總結(jié)
以上是生活随笔為你收集整理的JUnit 5扩展模型的生命周期的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: grep v grep_使用grep4j
- 下一篇: 电脑播放4k显卡要求(4K显示器显卡要求