How to force an error response from mocked-out SfsAdapter?


95
views
0
9 months ago by

In a service test,

SfsAdapter

 is mocked out. I need to force it to give a specified error response. How is this done?

modified 9 months ago by Graham Trott   • written 9 months ago by Graham Trott  

1 Answer


0
9 months ago by

The following code will return any desired error response:

final javax.ws.rs.core.Response response = mock(javax.ws.rs.core.Response.class);
when(response.getStatus()).thenReturn(Status.BAD_REQUEST.getStatusCode());
final List<com.clicktravel.schema.canonical.data.model.v1.common.Error> errorList = new ArrayList<>();
errorList.add(new com.clicktravel.schema.canonical.data.model.v1.common.Error()
        .description("That was a Bad Request"));
when(response.readEntity(ErrorResponse.class)).thenReturn(new ErrorResponse().errors(errorList));
when(sfsAdapter.cancelBooking(any())).thenReturn(response);

In this example a BAD_REQUEST response is given to cancelBooking().

modified 9 months ago by Graham Trott   • written 9 months ago by Graham Trott  

One place where you might need this code is if you get the following exception from readEntity():

java.lang.IllegalStateException: Method not supported on an outbound message.

written 9 months ago by Graham Trott  
Please log in to add an answer/comment or follow this question.

Similar posts:
Search »