For @Async
semantics to stick with, some active @Configuration
class will contain @EnableAsync
annotation , e.g.
@Configuration @EnableAsync @EnableScheduling public class AsyncConfiguration implements AsyncConfigurer {
To solve the problem, I introduced a new Spring non-async
profile.
If the non-async
profile is not active, AsyncConfiguration
used:
@Configuration @EnableAsync @EnableScheduling @Profile("!non-async") public class AsyncConfiguration implements AsyncConfigurer {
If a non-asynchronous profile is active Used by NonAsyncConfiguration
:
@Configuration // notice the missing @EnableAsync annotation @EnableScheduling @Profile("non-async") public class NonAsyncConfiguration { // this configuration will be active as long as profile "non-async" is active }
Now in the problematic JUnit test class, I explicitly activate the "non-async" profile to mutually exclude async behavior:
@RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) @WebAppConfiguration @IntegrationTest @Transactional @ActiveProfiles(profiles = "non-async") public class SomeServiceIntTest { @Inject private SomeService someService; @Test public void testAsyncMethod() { Foo testData = prepareTestData(); someService.asyncMethod(testData); verifyResults(); }
source share