1
我有一个类HttpClient
有一个返回CompletableFuture
功能:模拟CompletionException在测试
public class HttpClient {
public static CompletableFuture<int> getSize() {
CompletableFuture<int> future = ClientHelper.getResults()
.thenApply((searchResults) -> {
return searchResults.size();
});
return future;
}
}
然后另一个函数调用此函数:
public class Caller {
public static void caller() throws Exception {
// some other code than can throw an exception
HttpClient.getSize()
.thenApply((count) -> {
System.out.println(count);
return count;
})
.exceptionally(ex -> {
System.out.println("Whoops! Something happened....");
});
}
}
现在,我想写一个测试来模拟ClientHelper.getResults
失败,所以我写这个:
@Test
public void myTest() {
HttpClient mockClient = mock(HttpClient.class);
try {
Mockito.doThrow(new CompletionException(new Exception("HTTP call failed")))
.when(mockClient)
.getSize();
Caller.caller();
} catch (Exception e) {
Assert.fail("Caller should not have thrown an exception!");
}
}
此测试失败。 exceptionally
内的代码从未得到执行。但是,如果我正常运行源代码并且HTTP调用确实失败,那么它会很好地转到exceptionally
块。
我该如何编写测试以便执行exceptionally
代码?
我认为这是最好的方法:CompletableFuture是一个广泛使用和经过充分测试的库,因此您可以依靠它来测试代码,而不是尝试使用Mockito复制其行为。 (当然,Mockito是一个体面的方式来提供未来的系统测试中的依赖,您嘲笑。) –
谢谢@JeffBowman! –