java - Spring中如何使用JUNIT测试Async方法?
ringa_lee
ringa_lee 2017-04-17 17:32:50
0
1
635

使用Junit测试Spring中的Service层方法时,
@Async作为Annotation的异步方法应该如何测试?
在JUNIT测试中其直接作为异步执行,但是实际上却是没有执行成功(将rollback置为false数据库中没有持久化,调用了消息推送接口也没有进行推送,所以判断其没有完整执行)

那么在Spring中如何对异步方法进行测试?

ringa_lee
ringa_lee

ringa_lee

reply all(1)
小葫芦

http://jinnianshilongnian.iteye.com/blog/1901693
This blog explains it in more detail. The summary is:
1. When the @Aysnc method has no transaction, use Future.get( ) can obtain the results of the asynchronous thread in the main thread.
2. When @Async has a transaction, remove the asynchronous configuration in Spring's test configuration file so that @Async fails and is tested as synchronously.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!