Reputation: 1
I need to mock a class that has a method that returns javax.ws.rs.core.Response. The class follows this method call with a .readEntity(String.class). I'm setting the Response as follows:
when(methodCall).thenReturn(Response.status(Response.Status.OK).entity(someEntity).build());
Now when this Response is being read using response.readEntity(String.class); it throws an error:
java.lang.IllegalStateException : Method not supported on an outbound message.
How can I set the Response in thenReturn so as to return an inbound message readable by the readEntity() method.
P.S. I can't make changes in the main class, only in test class. I'm using java 1.8 with junit 4.12 and mockito-all 1.9.5, mockito-inline 3.8.0
The class I'm mocking:
Response response = client.target().request().header().post();
String entity = response.readEntity(String.class);
Upvotes: 0
Views: 1592
Reputation: 828
First of all, you have to be sure that the response is mocked:
Response response = Mockito.mock(Response.class)
Then I suggest you to mock readEntity
in this way:
Mockito.doReturn(Response.status(Response.Status.OK).entity(someEntity).build())
.when(response)
.readEntity(String.class);
Upvotes: -1