为了
@Async遵守语义,某些活动
@Configuration类将具有
@EnableAsync注释,例如
@Configuration@EnableAsync@EnableSchedulingpublic class AsyncConfiguration implements AsyncConfigurer { //}为了解决我的问题,我引入了一个新的Spring配置文件
non-async。
如果
non-async配置文件 未 激活,
AsyncConfiguration则使用:
@Configuration@EnableAsync@EnableScheduling@Profile("!non-async")public class AsyncConfiguration implements AsyncConfigurer { // this configuration will be active as long as profile "non-async" is not (!) active}如果非异步轮廓 是 活动的,则
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}现在,在有问题的JUnit测试类中,我显式激活“非异步”概要文件,以相互排除异步行为:
@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(); } // verifyResult() with assertions, etc.}


