我正在尝试进行一个集成测试,该测试将为void方法引发异常以模拟已关闭的服务。该方法具有字符串参数和作为参数的多部分文件,即使对具有两个字符串参数的void方法抛出异常,它似乎也不起作用。
工作集成测试:
@Test
@DisplayName("500 response -- downed case mgmt microservice")
public void downedCaseMgmt() throws Exception {
BDDMockito.doThrow(new RuntimeException("mocking an error")).when(reportEventService).reportDocUpload(ArgumentMatchers.any(String.class), ArgumentMatchers.anyString());
//Rest assured portion
given().
multiPart("file", xlsxGoodFile).
params(paramsMap).
when().
post("").
then().
statusCode(500);
}
非工作集成测试:
@Test
@DisplayName("500 response -- downed object storage")
public void downedObjectStorage() throws Exception {
BDDMockito.doThrow(new RuntimeException("mocking an error")).when(objectStorageService).saveFileToObjectStorage(ArgumentMatchers.anyString(), ArgumentMatchers.any(File.class));
//Rest assured portion
given().
multiPart("file", xlsxGoodFile).
params(paramsMap).
when().
post("").
then().
statusCode(500);
}
事实是,由于objectStorageService上的模拟bean以及我正在模拟返回的事实,因此函数saveFileToObjectStorage具有空值。我的错误,我使用以下代码解决了:
@Test
@DisplayName("500 response -- downed db")
public void downedDb() throws Exception {
BDDMockito.doThrow(new RuntimeException("mocking an error")).when(excelDataRepository).
save(ArgumentMatchers.any());
//Rest assured portion
given().
multiPart("file", xlsxGoodFile).
params(paramsMap).
when().
post("").
then().
statusCode(500);
}
注意:ArgumentMatchers的any()