如何输入和测试catch子句.春靴,Junit,Mockito



我正在尝试测试以下代码部分。尤其是陷阱:

try {
preAuthCompleteResponse = preAuthCompleteClient
.getPreAuthCompleteResponse(preAuthCompleteServiceImpl.getPreAuthComplete(
preAuthCompleteRequestServiceImpl.getPreAuthCompleteRequest(preAuthCompleteModel)));
} catch (IOException | MarshalSendAndReceiveException e) {
logger.error(e.getMessage(), e);
return new ResponseEntity<>(
new SvsTransactionResponseModel(HttpStatus.BAD_REQUEST, e.getMessage()),
HttpStatus.BAD_REQUEST);
}

在一个测试中,我希望捕获IOException,在另一个测试中将捕获MarshalSendAndReceiveException。然后两者都返回状态为HttpStatus.BAD_REQUESTSvsTransactionResponseModel

preAuthCompleteClient.getPreAuthCompleteResponse方法抛出MarshalSendAndReceiveExceptionpreAuthCompleteRequestServiceImpl.getPreAuthCompleteRequest抛出IOException

我的测试是这样的:

@RunWith(MockitoJUnitRunner.class)
public class PreAuthCompleteControllerTest {
@Mock
private PreAuthCompleteService preAuthCompleteServiceImpl;
@Mock
private PreAuthCompleteRequestService preAuthCompleteRequestServiceImpl;
@Mock
private PreAuthCompleteClient preAuthCompleteClient;
@Mock
private Errors errors;
private PreAuthCompleteController preAuthCompleteController;
@Before
public void setUp() {
preAuthCompleteController = new PreAuthCompleteController(preAuthCompleteServiceImpl,
preAuthCompleteRequestServiceImpl, preAuthCompleteClient);
}
@Test
public void testGetPreAuthCompleteExpectIOException() throws MarshalSendAndReceiveException, IOException {
when(preAuthCompleteClient.getPreAuthCompleteResponse(preAuthCompleteServiceImpl.getPreAuthComplete(
preAuthCompleteRequestServiceImpl.getPreAuthCompleteRequest(new PreAuthCompleteModel()))))
.thenReturn(new PreAuthCompleteResponse());
ResponseEntity<SvsTransactionResponseModel> responseEntity = (ResponseEntity<SvsTransactionResponseModel>) preAuthCompleteController
.getPreAuthComplete(null, errors);
assertTrue(responseEntity.getBody() != null);
assertTrue(responseEntity.getStatusCodeValue() == 400);
}
}

我使用Mockito.when、Mockito.thenThrow或doThrow等尝试了不同的解决方案。但得到了不同的异常或不成功的测试。我没有主意了。

如何测试异常是否被捕获以及ResponseEntity是否正确返回。

附言:所有的模拟配置都有效。

为了帮助理解我打破了这个调用。。。

preAuthCompleteResponse = preAuthCompleteClient
.getPreAuthCompleteResponse(preAuthCompleteServiceImpl.getPreAuthComplete(
preAuthCompleteRequestServiceImpl.getPreAuthCompleteRequest(preAuthCompleteModel)));

分解为其组成部分:

PreAuthCompleteRequest preAuthCompleteRequest = preAuthCompleteRequestServiceImpl.getPreAuthCompleteRequest(preAuthCompleteModel);
PreAuthComplete preAuthComplete = preAuthCompleteServiceImpl.getPreAuthComplete(preAuthCompleteRequest);
PreAuthCompleteResponse preAuthCompleteResponse = preAuthCompleteClient.getPreAuthCompleteResponse(preAuthComplete);

我在猜测返回类型(PreAuthCompleteRequestPreAuthComplete等),但你已经明白了(我想:)。给定上述调用顺序,应通过以下测试:

@Test
public void badRequestWhenPreAuthCompleteResponseFails() {
// preAuthCompleteController.getPreAuthComplete() invokes 
//    preAuthCompleteClient.getPreAuthCompleteResponse() 
// and any MarshalSendAndReceiveException throw by that client should result in a BAD_REQUEST
MarshalSendAndReceiveException expectedException = new MarshalSendAndReceiveException("boom!");
when(preAuthCompleteClient.getPreAuthCompleteResponse(Mockito.any(PreAuthComplete.class))).thenThrow(expectedException);

ResponseEntity<SvsTransactionResponseModel> responseEntity = (ResponseEntity<SvsTransactionResponseModel>) preAuthCompleteController
.getPreAuthComplete(null, errors);
assertTrue(responseEntity.getBody() != null);
assertTrue(responseEntity.getStatusCodeValue() == 400);
}
@Test
public void badRequestWhenPreAuthCompleteRequestFails() {
// preAuthCompleteController.getPreAuthComplete() invokes
//    preAuthCompleteClient.getPreAuthCompleteResponse() which then invokes
//    preAuthCompleteServiceImpl.getPreAuthComplete() which then invokes 
//    preAuthCompleteRequestServiceImpl.getPreAuthCompleteRequest
// and any IOException throw by that call should result in a BAD_REQUEST
IOException expectedException = new IOException("boom!");
when(preAuthCompleteRequestServiceImpl.getPreAuthCompleteRequest(Mockito.any(PreAuthCompleteModel.class))).thenThrow(expectedException);
ResponseEntity<SvsTransactionResponseModel> responseEntity = (ResponseEntity<SvsTransactionResponseModel>) preAuthCompleteController
.getPreAuthComplete(null, errors);
assertTrue(responseEntity.getBody() != null);
assertTrue(responseEntity.getStatusCodeValue() == 400);
}

相关内容

  • 没有找到相关文章

最新更新