junit5测试生命周期回调【进阶】

回调接口

  • BeforeAllCallback
  • AfterAllCallback
  • BeforeEachCallBack
  • AfterEachCallback
  • BeforeTestExecutionCallback
  • AfterTestExecutionCallback

BeforeAllCallback

  • 所有测试方法 及 @BeforeAll@AfterAll 注解修饰的方法 执行之前和之后执行

AfterAllCallback

  • 所有测试方法 及 @BeforeAll@AfterAll 注解修饰的方法 执行之前和之后执行

BeforeEachCallBack

  • 每个测试方法及 @BeforeEach@AfterEach 注解修饰的方法 之前和之后执行

AfterEachCallback

  • 每个测试方法及 @BeforeEach@AfterEach 注解修饰的方法 之前和之后执行

BeforeTestExecutionCallback

  • 在测试方法@Test 之前和之后立即执行

AfterTestExecutionCallback

  • 在测试方法@Test 之前和之后立即执行

执行顺序

  1. BeforeAllCallback
  2. BeforeAll
  3. BeforeEachCallback
  4. BeforeEach
  5. BeforeTestExecutionCallback
  6. Test
  7. AfterTestExecutionCallback
  8. AfterEach
  9. AfterEachCallback
  10. AfterAll
  11. AfterAllCallback

回调激活

  • @ExtendWith 注解
package com.junit5.lifecycle_callback;

import org.junit.jupiter.api.extension.*;

public class CallExtension implements BeforeAllCallback, AfterAllCallback, BeforeEachCallback,
        AfterEachCallback, BeforeTestExecutionCallback, AfterTestExecutionCallback {
    @Override
    public void beforeAll(ExtensionContext extensionContext) throws Exception {
        System.out.println("CallExtension--BeforeAllCallback--beforeAll");
    }

    @Override
    public void afterAll(ExtensionContext extensionContext) throws Exception {
        System.out.println("CallExtension--AfterAllCallback--afterAll");
    }


    @Override
    public void beforeEach(ExtensionContext extensionContext) throws Exception {
        System.out.println("CallExtension--BeforeEachCallback--beforeEach");
    }

    @Override
    public void afterEach(ExtensionContext extensionContext) throws Exception {
        System.out.println("CallExtension--AfterEachCallback--afterEach");
    }

    @Override
    public void beforeTestExecution(ExtensionContext extensionContext) throws Exception {
        System.out.println("CallExtension--BeforeTestExecutionCallback--beforeTestExecution");
    }

    @Override
    public void afterTestExecution(ExtensionContext extensionContext) throws Exception {
        System.out.println("CallExtension--AfterTestExecutionCallback--afterTestExecution");
    }
}

package com.junit5.lifecycle_callback;

import org.junit.jupiter.api.extension.AfterTestExecutionCallback;
import org.junit.jupiter.api.extension.BeforeTestExecutionCallback;
import org.junit.jupiter.api.extension.ExtensionContext;

import java.lang.reflect.Method;

public class TimeExtension implements BeforeTestExecutionCallback, AfterTestExecutionCallback {

    @Override
    public void beforeTestExecution(ExtensionContext extensionContext) throws Exception {
        Method testMethod = extensionContext.getRequiredTestMethod();
        extensionContext.getStore(
                ExtensionContext.Namespace.create(
                        getClass(),testMethod
                )
        ).put("Start-Time",System.currentTimeMillis());
    }

    @Override
    public void afterTestExecution(ExtensionContext extensionContext) throws Exception {
        Method testMethod = extensionContext.getRequiredTestMethod();
        Long startTime = extensionContext.getStore(
                ExtensionContext.Namespace.create(
                        getClass(), testMethod
                )
        ).remove("Start-Time", long.class);

        Long endTime = System.currentTimeMillis();
        Long durTime = endTime-startTime;//测试方法运行时间
        System.out.println("方法名为:"+testMethod.getName()+"持续时间为:"+durTime+"ms");
    }

}

package com.junit5.lifecycle_callback;

import org.junit.jupiter.api.*;
import org.junit.jupiter.api.extension.ExtendWith;

@ExtendWith({
        CallExtension.class,
        TimeExtension.class
})
public class OneTest {
    @BeforeAll
    public static void bf(){
        System.out.println("OneTest----BeforeAll");
        System.out.println("数据库中---创建用户表");
    }

    @BeforeEach
    public void be(){
        System.out.println("OneTest----BeforeEach");
        System.out.println("数据库中---提示说明对表的数据要进行操作");
    }

    @Test
    public void test1(){
        System.out.println("数据库中----添加用户的业务逻辑");
    }

    @Test
    public void test2(){
        System.out.println("数据库中----更新用户手机号的业务逻辑");
    }


    @AfterEach
    public void ae(){
        System.out.println("OneTest----AfterEach");
        System.out.println("数据库中---更新数据时事务提交");
    }

    @AfterAll
    public static void af(){
        System.out.println("OneTest----AfterAll");
        System.out.println("数据库中---删除用户表");
    }
}