Patrick
Patrick

Reputation: 12734

How to enter and test catch clause. Spring-boot, Junit, Mockito

I'm trying to test the following code part. Especially the catch:

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);
    }

In one test I want expect that a IOException is catched and in the other test I want expect that a MarshalSendAndReceiveException is catched. Both return then the SvsTransactionResponseModel with status HttpStatus.BAD_REQUEST.

The preAuthCompleteClient.getPreAuthCompleteResponse method throws a MarshalSendAndReceiveException and preAuthCompleteRequestServiceImpl.getPreAuthCompleteRequest throws an IOException.

My test looks like that:

@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);
    }

}

I tried different solution using Mockito.when, Mockito.thenThrow or doThrow and so on. But got different exceptions or unsuccesful tests. I am out of ideas.

How can I test that the exceptions are catched and the ResponseEntity return correctly.

P.S. All the mock config work.

Upvotes: 0

Views: 1356

Answers (1)

glytching
glytching

Reputation: 47865

To help make sense of things I have broken this call ...

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

... down into its constituent parts:

PreAuthCompleteRequest preAuthCompleteRequest = preAuthCompleteRequestServiceImpl.getPreAuthCompleteRequest(preAuthCompleteModel);

PreAuthComplete preAuthComplete = preAuthCompleteServiceImpl.getPreAuthComplete(preAuthCompleteRequest);

PreAuthCompleteResponse preAuthCompleteResponse = preAuthCompleteClient.getPreAuthCompleteResponse(preAuthComplete);

I'm guessing about the return types (PreAuthCompleteRequest, PreAuthComplete etc) but you get the idea (I think :). Given the above call sequence, the following tests should pass:

@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);
}

Upvotes: 1

Related Questions