首页 > 解决方案 > SpringBoot 中 @Async 注释的组件/集成测试失败

问题描述

在其中一个类中包含 @Async 注释的 SpringBoot 项目中运行组件/集成测试时遇到错误。

当我删除 @Async 注释时,它工作正常。

我的代码简要如下所示:

@Configuration
@EnableAsync
public class MyAsyncConfiguration {

  @Bean
  public Executor threadPoolTaskExecutor() {
    return new ThreadPoolTaskExecutor();
  }

}

@Async 注解的方法如下所示:

@Service
public class AsyncOperations {

  @Async
  public void doAsyncOperations() {
    // Some business logic here
  }
    

当我从 Postman 访问我的 API 时,代码运行良好。异步操作按预期异步发生,但是当我使用 mvn verify 命令运行组件/集成测试时,出现如下错误:

Caused by: Java.lang.IllegalStateException:Failed to introspect class [className] from ClassLoader [jdk.internal.loader.ClassLoader]

Caused by: java.lang.ClassFormatError: Method "$jacocoData" in class (AsyncOperations) has illegal signature

标签: springspring-bootintegration-testingspring-boot-test

解决方案


推荐阅读