Is it possible to disable Spring's @Async duri

2019-06-28 05:21发布

问题:

I have a method annotated with @Async say

@Async public void createArticles(long Id){}

but I'm just trying to a cucumber test of that method. Is it possible to test it synchronously?

回答1:

Create your async configuration class:

@Configuration
@EnableAsync
@ConditionalOnProperty(name = "async.enabled", havingValue = "true")
public class SpringAsyncConfig {
    // Your configuration here
}

the annotation @ConditionalOnProperty here will create this bean if only the configuration value async.enabled is true

Then in the configuration file if you are going to run the integration test you can set the value to false

async.enabled = false

or if you are using Spring profiles you can always set the value to true in other profiles configuration files like application.properties, application-dev and set to false in application-integration.properties