Micronaut测试报错:实例化Bean时缺少@Introduction方法拦截器
问题描述
执行Micronaut集成测试时,Kafka客户端接口VerificationSender的send方法抛出拦截器缺失异常。
错误信息
Message: At least one @Introduction method interceptor required, but missing for method: void send(String key,VerificationRequest request). Check if your @Introduction stereotype annotation is marked with @Retention(RUNTIME) and @InterceptorBean(..) with the interceptor type. Otherwise do not load @Introduction beans if their interceptor definitions are missing!
相关代码
Kafka客户端接口
@KafkaClient(id = "sender") fun interface VerificationSender { @Topic("\${$VERIFICATION_TOPIC_NAME_PROPERTY}") fun send(@KafkaKey key: String, request: VerificationRequest) }
集成测试注解定义
@Target(CLASS, FUNCTION) @Retention(RUNTIME) @MustBeDocumented @Test @TagIntegrationTest annotation class IntegrationTest
完整错误栈
At least one @Introduction method interceptor required, but missing for method: void send(String key,VerificationRequest request). Check if your @Introduction stereotype annotation is marked with @Retention(RUNTIME) and @InterceptorBean(..) with the interceptor type. Otherwise do not load @Introduction beans if their interceptor definitions are missing! java.lang.IllegalStateException: At least one @Introduction method interceptor required, but missing for method: void send(String key,VerificationRequest request). Check if your @Introduction stereotype annotation is marked with @Retention(RUNTIME) and @InterceptorBean(..) with the interceptor type. Otherwise do not load @Introduction beans if their interceptor definitions are missing! at io.micronaut.aop.chain.DefaultInterceptorRegistry.resolveToNone(DefaultInterceptorRegistry.java:108) at io.micronaut.aop.chain.DefaultInterceptorRegistry.resolveInterceptors(DefaultInterceptorRegistry.java:71) at io.micronaut.aop.chain.InterceptorChain.resolveInterceptors(InterceptorChain.java:259) at io.micronaut.aop.chain.InterceptorChain.resolveInterceptors(InterceptorChain.java:251) at io.micronaut.aop.chain.InterceptorChain.resolveIntroductionInterceptors(InterceptorChain.java:171)
解决方案
原因分析
@KafkaClient是Micronaut的Introduction型注解,需要对应的拦截器生成代理类实现客户端逻辑。测试环境中可能缺少Kafka客户端相关依赖或自动配置,导致拦截器未被加载。
具体解决步骤
补全测试依赖
确保构建文件中包含Micronaut Kafka测试依赖:// Gradle示例 testImplementation("io.micronaut.kafka:micronaut-kafka-test")或Maven:
<!-- Maven示例 --> <dependency> <groupId>io.micronaut.kafka</groupId> <artifactId>micronaut-kafka-test</artifactId> <scope>test</scope> </dependency>完善测试注解配置
自定义的@IntegrationTest注解需要包含@MicronautTest,确保测试上下文加载Kafka相关配置:@Target(CLASS, FUNCTION) @Retention(RUNTIME) @MustBeDocumented @MicronautTest @Test @TagIntegrationTest annotation class IntegrationTest配置测试环境
在测试配置文件(如src/test/resources/application.yml)中指定Kafka地址,或使用嵌入式Kafka:kafka: bootstrap: servers: localhost:9092若使用嵌入式Kafka,可在测试类上添加
@EmbeddedKafka注解自动启动:@IntegrationTest @EmbeddedKafka class VerificationSenderTest { // 测试代码 }检查自动配置排除项
确保配置文件中没有排除Kafka相关自动配置:# 若存在以下配置需删除 micronaut: context: exclude: - io.micronaut.kafka.*
内容的提问来源于stack exchange,提问作者user3497321

