Spring Boot Actuator 2.6.6 (initialising bean error)

Viewed 1592

I get the following error message when trying to run my Spring Boot 2.6.6 project test cases with actuator:

Parameter 0 of method bindTaskExecutorsToRegistry in org.springframework.boot.actuate.autoconfigure.metrics.task.TaskExecutorMetricsAutoConfiguration required a bean of type 'java.util.Map' that could not be found.

Again, this issue only presents itself when trying to run @SpringBootTest integration tests.

The @Autowired method:

  @Autowired
  public void bindTaskExecutorsToRegistry(Map<String, Executor> executors, MeterRegistry registry) {
    executors.forEach((beanName, executor) -> {
      if (executor instanceof ThreadPoolTaskExecutor) {
        this.monitor(registry, this.safeGetThreadPoolExecutor((ThreadPoolTaskExecutor)executor), beanName);
      } else if (executor instanceof ThreadPoolTaskScheduler) {
        this.monitor(registry, this.safeGetThreadPoolExecutor((ThreadPoolTaskScheduler)executor), beanName);
      }

    });
  }

EDIT: Minimal reproducible project.

There is a configuration that exposes a ThreadPoolTaskExecutor for logging purposes.

@Configuration
@EnableAsync
public class DemoConfiguration {

  @Bean
  public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(10);
    executor.setMaxPoolSize(20);
    executor.setQueueCapacity(500);
    executor.setThreadNamePrefix("Thread");
    executor.setWaitForTasksToCompleteOnShutdown(true);
    executor.initialize();
    return executor;
  }
}

When this configuration is injected into a @SpringTest, an exception is thrown:

@SpringBootTest
class DemoApplicationTests {

  @MockBean
  DemoConfiguration configuration;

  @Test
  void contextLoads() {
    // User configuration.
  }
}
1 Answers

A workaround was to add the following @MockBean:

  @MockBean private Executor executor;
Related