我想添加方法测试,其中包含 CompletableFuture:
public void report(List<String> srcList) {
if (srcList != null) {
...
CompletableFuture.runAsync(() ->
....
srcList.forEach(src-> downloader.send(url)));
}
}
Run Code Online (Sandbox Code Playgroud)
我想测试一下,那个方法send
被调用了。我的测试看起来像:
@Test
public void _test() {
List<String> events = new ArrayList();
events.add("http://xxxx//");
events.add("http://xxxx//");
expect(downloader.send(events.get(0))).andReturn("xxx").times(2);
replay(downloader);
eventReporter.report(events);
verify(downloader);
}
Run Code Online (Sandbox Code Playgroud)
我得到这样的错误Downloader.send("http://xxxx//"): expected: 2, actual: 0
避免此错误的一种方法是设置Thread.sleep(100);
超时。然后线程将等待并验证该方法是否已调用。但这会增加测试时间。
还有其他方法可以使用 EasyMock 测试多线程吗?