SpringBoot单元测试时不运行kafka、rabbitmq等队列消费者
SpringBoot单元测试时屏蔽kafka、rabbitmq等队列消费者
# 在单元测试的class上加上该注解即可
@TypeExcludeFilters({ListenerExcludeFilter.class})
public class AppTest {
# .......
}
ListenerExcludeFilter.class代码
public class ListenerExcludeFilter extends TypeExcludeFilter {
# rabbitmq同理
private static final String KAFKA_LISTENER = "org.springframework.kafka.annotation.KafkaListener";
private static final String KAFKA_HANDLER = "org.springframework.kafka.annotation.KafkaHandler";
@Override
public boolean match(MetadataReader metadataReader,
MetadataReaderFactory metadataReaderFactory) {
AnnotationMetadata annotationMetadata =
metadataReader.getAnnotationMetadata();
return annotationMetadata.hasAnnotatedMethods(KAFKA_LISTENER)
|| annotationMetadata.hasAnnotatedMethods(KAFKA_HANDLER);
}
@Override
public boolean equals(Object o) {
return o != null && getClass() == o.getClass();
}
@Override
public int hashCode() {
return 1;
}
}