Java – Disables @Async calls in the test context

Disables @Async calls in the test context… here is a solution to the problem.

Disables @Async calls in the test context

I’m trying to test the void method with @Async comments. Like this:

    @Async
    public void asyncMethod()
    {
        some actions
    }

I know it’s okay to turn off anync execution in tests without adding @EnableConfig to my test spring config.
The problem is that my test Spring configuration class contains .xml configuration. And the asynchronous call property is enabled in that configuration.
Here is my test config class:

@Configuration
@ImportResource(value = {/some paths to xml contexts})
@ComponentScan(basePackages = { "package" })
public class TestApplication
{
     beans
}

This is how asynchronous calls are turned on in one of the XML configurations:

<task:annotation-driven executor="parallelUpdateExecutor" scheduler="scheduler" mode="aspectj"/>

I’m interested, is there the ability to turn off async calls in a test spring context?
Thank you.

Solution

The easiest way to do this is to define a configuration file using beans.

You can put <task:annotation-drive.... Nested declarations <beans profile="async".... section in your XML file, and then activate the profile only through @ActiveProfiles ("async") for tests that need it

Greetings

Sam (author of the Spring TestContext Framework).

Related Problems and Solutions