1

I'm writing tests for my Async method, annotated with @Async. I'm trying to disable Async by using below configuration class:

@Profile("!integ-test")
@Configuration
@EnableAsync
public class AsyncConfiguration {}

Calling code:

@Autowired private ApiDelegate apiDelegate;

apiDelegate.exportResources(searchContext);

Method:

 @Async
 public void exportResources(
  SearchContext searchContext,
  ) {}

Spring boot version: 2.3.12.RELEASE

The problem is the method is still being executed asynchronously. There is no place else where @EnableAsync is placed. I've also tried removing @EnableAsync altogether, but the method remains asynchronous. There seems to be no effect of removing @EnableAsync. Please help. Thanks.

Aditya Narayan Dixit
  • 2,105
  • 11
  • 23
  • 2
    Please show how you are executing the code as well as specifying which Spring Boot version you are using. Are you by any chance using `spring-boot-starter-actuator` as that will enable asyn and scheduling by default. – M. Deinum May 19 '22 at 06:38
  • @M.Deinum Updated my question – Aditya Narayan Dixit May 19 '22 at 06:45
  • Yes, I have `org.springframework.boot:spring-boot-starter-actuator` in dependencies. – Aditya Narayan Dixit May 19 '22 at 06:46
  • Well if you have that async and scheduling is enabled by default as that is needed by actuator. – M. Deinum May 19 '22 at 06:47
  • 1
    Thanks @M.Deinum. Is there any way I can override that behaviour? – Aditya Narayan Dixit May 19 '22 at 06:52
  • How do you know that it's being executed asynchronously? Are you sure you run your code with profile "integ-test" enabled? – dekkard May 19 '22 at 07:38
  • Yes, I have "integ-test" profile active. Have debugged the test to identify the method is being executed asynchronously @dekkard – Aditya Narayan Dixit May 19 '22 at 07:41
  • @M.Deinum do you have any links with the info on `spring-boot-starter-actuator` activating async processing? Just tried to repro this in an empty Spring boot 2.6 project and an `@Async` method is executed in the same thread as a caller if `@EnableAsync` is not specified in a configuration. – dekkard May 19 '22 at 07:52
  • If you don't use the actuator it won't enable it. It might depend on which exporter you use for exposing your metrics. – M. Deinum May 19 '22 at 08:05

0 Answers0