Issue
I am trying to use JUnit4 test a utility method that takes a javax.ws.rs.core.Response as an input parameter. The method looks at the Response attributes and takes some action based on the response code. When I just create a response in my test method and submit it using:
Response resp = Response.status(Status.INTERNAL_SERVER_ERROR).entity(respMessage).build();
ResponseParser.parseResponse(resp);
My utility method throws:
java.lang.IllegalStateException: RESTEASY003290: Entity is not backed by an input stream
at org.jboss.resteasy.specimpl.BuiltResponse.readEntity(BuiltResponse.java:231)
at org.jboss.resteasy.specimpl.BuiltResponse.readEntity(BuiltResponse.java:218)
My util class is basically:
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status.Family;
public class ResponseParser {
public static void parseResponse(Response response) {
String errorResp = "";
if (!response.getStatusInfo().getFamily().equals(Family.SUCCESSFUL)) {
int errorCode = response.getStatus();
errorResp = response.readEntity(String.class);
}
}
}
How can I create a response that will be properly backed by a stream as expected by resteasy? I looked at quite a few similar questions but they mostly seemed to be centered around testing or mocking an endpoint call. I just want to test my parsing method.
Solution
Mock the Response with some unit testing framework, like Mockito. Then you will be able to declare what to return on each response method call, or even check if a method has been called.
Answered By - albert_nil
Answer Checked By - Cary Denson (JavaFixing Admin)