当前位置: 首页 > article >正文

JUnit 5 中获取测试类、测试方法及属性的注解


JUnit 5 中获取测试类、测试方法及属性的注解

JUnit 5 提供了强大的扩展机制,允许通过 ExtensionContext 获取测试类、测试方法及其属性上的注解信息。以下是具体实现方法和示例:


一、核心 API
  1. ExtensionContext

    • 提供测试执行的上下文信息,包括测试类、方法、注解等。
    • 通过扩展接口(如 BeforeEachCallbackParameterResolver)访问。
  2. AnnotatedElement

    • 表示可注解的元素(如类、方法、字段)。
    • 提供 getAnnotation()getAnnotations() 方法获取注解。

二、获取注解的步骤
  1. 实现扩展接口
    选择适合的扩展接口(如 BeforeEachCallbackTestExecutionCondition)。
  2. 通过 ExtensionContext 获取元素
    使用 getRequiredTestClass()getRequiredTestMethod() 等方法。
  3. 获取注解信息
    调用 getAnnotation()getAnnotations() 方法。

三、具体示例
示例 1:获取测试类上的注解
import org.junit.jupiter.api.extension.*;

public class ClassAnnotationExtension implements BeforeEachCallback {

    @Override
    public void beforeEach(ExtensionContext context) {
        // 获取测试类
        Class<?> testClass = context.getRequiredTestClass();
        
        // 获取类上的注解
        DisplayName displayName = testClass.getAnnotation(DisplayName.class);
        if (displayName != null) {
            System.out.println("测试类显示名称: " + displayName.value());
        }

        Tag tag = testClass.getAnnotation(Tag.class);
        if (tag != null) {
            System.out.println("测试类标签: " + tag.value());
        }
    }
}

// 使用扩展
@DisplayName("用户服务测试")
@Tag("service")
@ExtendWith(ClassAnnotationExtension.class)
class UserServiceTest {
    @Test
    void testMethod() {
        Assertions.assertTrue(true);
    }
}

输出

测试类显示名称: 用户服务测试
测试类标签: service

示例 2:获取测试方法上的注解
import org.junit.jupiter.api.extension.*;

public class MethodAnnotationExtension implements BeforeEachCallback {

    @Override
    public void beforeEach(ExtensionContext context) {
        // 获取测试方法
        Method testMethod = context.getRequiredTestMethod();
        
        // 获取方法上的注解
        DisplayName displayName = testMethod.getAnnotation(DisplayName.class);
        if (displayName != null) {
            System.out.println("测试方法显示名称: " + displayName.value());
        }

        Tag tag = testMethod.getAnnotation(Tag.class);
        if (tag != null) {
            System.out.println("测试方法标签: " + tag.value());
        }
    }
}

// 使用扩展
@ExtendWith(MethodAnnotationExtension.class)
class UserServiceTest {

    @Test
    @DisplayName("测试用户保存")
    @Tag("save")
    void testSaveUser() {
        Assertions.assertTrue(true);
    }
}

输出

测试方法显示名称: 测试用户保存
测试方法标签: save

示例 3:获取测试类属性上的注解
import org.junit.jupiter.api.extension.*;

public class FieldAnnotationExtension implements BeforeEachCallback {

    @Override
    public void beforeEach(ExtensionContext context) {
        // 获取测试类
        Class<?> testClass = context.getRequiredTestClass();
        
        // 遍历所有字段
        for (Field field : testClass.getDeclaredFields()) {
            // 获取字段上的注解
            Mock mock = field.getAnnotation(Mock.class);
            if (mock != null) {
                System.out.println("找到 Mock 字段: " + field.getName());
            }
        }
    }
}

// 使用扩展
@ExtendWith(FieldAnnotationExtension.class)
class UserServiceTest {

    @Mock
    private UserRepository userRepository;

    @Test
    void testMethod() {
        Assertions.assertTrue(true);
    }
}

输出

找到 Mock 字段: userRepository

示例 4:获取所有注解
import org.junit.jupiter.api.extension.*;

public class AllAnnotationsExtension implements BeforeEachCallback {

    @Override
    public void beforeEach(ExtensionContext context) {
        // 获取测试类
        Class<?> testClass = context.getRequiredTestClass();
        
        // 获取类上的所有注解
        Annotation[] classAnnotations = testClass.getAnnotations();
        System.out.println("测试类注解: " + Arrays.toString(classAnnotations));

        // 获取测试方法
        Method testMethod = context.getRequiredTestMethod();
        
        // 获取方法上的所有注解
        Annotation[] methodAnnotations = testMethod.getAnnotations();
        System.out.println("测试方法注解: " + Arrays.toString(methodAnnotations));
    }
}

// 使用扩展
@DisplayName("用户服务测试")
@Tag("service")
@ExtendWith(AllAnnotationsExtension.class)
class UserServiceTest {

    @Test
    @DisplayName("测试用户保存")
    @Tag("save")
    void testSaveUser() {
        Assertions.assertTrue(true);
    }
}

输出

测试类注解: [@org.junit.jupiter.api.DisplayName(value=用户服务测试), @org.junit.jupiter.api.Tag(value=service)]
测试方法注解: [@org.junit.jupiter.api.DisplayName(value=测试用户保存), @org.junit.jupiter.api.Tag(value=save)]

四、总结

通过 ExtensionContext 和反射 API,可以轻松获取测试类、方法和属性上的注解信息,适用于以下场景:

  • 动态配置测试:根据注解值调整测试行为。
  • 生成测试报告:提取注解信息生成详细报告。
  • 扩展功能:实现自定义注解处理器(如依赖注入、条件测试)。

建议结合具体需求选择合适的扩展接口,并注意注解的继承和组合特性。


http://www.kler.cn/a/538376.html

相关文章:

  • 【Java八股】JVM
  • Kafka中的KRaft算法
  • python连点器
  • [Meet DeepSeek] 如何顺畅使用DeepSeek?告别【服务器繁忙,请稍后再试。】
  • 如何通过 ESPN API 获取 NBA 球队的赛程表
  • Day 32 卡玛笔记
  • DeepSeek与Vue.js组件开发:解锁AI与前端开发的融合密码
  • 算法兵法全略(译文)
  • 低代码系统-插件功能分析( 某道云)
  • 线性dp-安全序列
  • 指向深度学习的“信息技术”课程单元教学设计方案
  • 数据表中的视图操作
  • 激活函数篇 03 —— ReLU、LeakyReLU、RandomizedLeakkyReLU、PReLU、ELU
  • 如何参与开源项目
  • 33.日常算法
  • Python进阶-在Ubuntu上部署Flask应用
  • iPhone 在华销量大幅下挫
  • STM32启动过程概述
  • Elasticsearch term精确查询无数据
  • Maven 依赖范围与排除
  • 如何训练开源模型成为专业业务模型
  • Racecar Gym 总结
  • DeepSeek训练成本与技术揭秘
  • android中关于CheckBox自定义选中图片选中无效问题
  • 京准:NTP卫星时钟服务器对于DeepSeek安全的重要性
  • ChatGPT搜索免费开放:AI搜索引擎挑战谷歌霸主地位全面分析