栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

JUnit测试Spring @Async void服务方法

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JUnit测试Spring @Async void服务方法

为了

@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.}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/428393.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号