We ended up using a prop yourcompany.someExecutor.async
that we default to true
(so it does not show up in the application.yml
) and in a test we set it to false
using TestPropertySource
. Based on that prop we either initialize a SyncTaskExecutor
or some async version (e.g. ThreadPoolTaskExecutor
).
Note that this also enables to use multiple props so it is easy to disable a specific executor per prop. In our case we have several async executers depending on the context.
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@TestPropertySource(properties = {
"yourcompany.someExecutor.async=false",
})
public class SomeIntegrationTest {
// ... tests requiring async disabled
}
@Configuration
public class SomeConfig {
// ...
@Value("${yourcompany.someExecutor.async:true}")
private boolean asyncEnabled;
@Bean("someExecutor") // specific executor
public Executor algoExecutor() {
if (!asyncEnabled) {
return new SyncTaskExecutor();
}
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(THREAD_COUNT);
executor.setMaxPoolSize(THREAD_COUNT);
executor.setQueueCapacity(QUEUE_CAPACITY);
executor.setThreadNamePrefix("Some-");
executor.initialize();
return executor;
}
}