扩展 junit 框架_JUnit 5扩展模型的生命周期
擴展 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// <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
擴展 junit 框架
總結
以上是生活随笔為你收集整理的扩展 junit 框架_JUnit 5扩展模型的生命周期的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: gmg小绿人买的游戏怎么激活
- 下一篇: Hibernate架构概述