鍍金池/ 問答/Java  測試  網(wǎng)絡(luò)安全/ 集成測試中如何測試異步邏輯?

集成測試中如何測試異步邏輯?

代碼中存在異步邏輯

@Autowired
AsyncRunner asyncRunner;
...
public Result testMethod() {
    ...
    asyncRunner.submit(()->{
    // 一些異步邏輯
    });
    ...
}

如果直接測試的話,異步方法來不及執(zhí)行完整?,F(xiàn)在是粗暴地通過Thread.sleep()來測試,有沒有辦法能夠借助mock,使得異步邏輯在測試中同步執(zhí)行?
期望的方法如:

@Mock
AynscRunner asyncRunner;
...
when(asyncRunner.submit(testLogic())).callRealMethod(testLogic());
Result result = testMethod();
...
回答
編輯回答
有你在
2017年11月19日 01:50