JIRAIYA
JIRAIYA

Reputation: 1

Mockito mocking a Response class and reading with readEntity method

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

Answers (1)

frascu
frascu

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

Related Questions