我有一个具有返回列表 future 的外部依赖项的类。 如何模拟外部依赖?
public void meth() {
//some stuff
Future<List<String>> f1 = obj.methNew("anyString")
//some stuff
}
when(obj.methNew(anyString()).thenReturn("how to intialise some data here, like list of names")
最佳答案
您可以使用 thenReturn()
创建 future 并返回它.在下面的例子中,我创建了一个已经完成的 Future<List<String>>
使用 CompletableFuture
.
when(f1.methNew(anyString()))
.thenReturn(CompletableFuture.completedFuture(Arrays.asList("A", "B", "C")));
关于java - 模拟返回列表 future 的外部依赖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45381731/