Class JakartaRestTestHelpers
java.lang.Object
org.kiwiproject.beta.test.jersey.ws.rs.JakartaRestTestHelpers
Test utilities for Jakarta RESTful Web Services.
-
Method Summary
Modifier and TypeMethodDescriptionstatic jakarta.ws.rs.core.ResponsetoInboundResponse(jakarta.ws.rs.core.Response outboundResponse) Converts an outboundResponsebuilt as if from inside a Jakarta EE resource, e.g., something likeResponse.ok(entity).build(), into an inbound Response on whichResponse.readEntity(Class)can be called.
-
Method Details
-
toInboundResponse
public static jakarta.ws.rs.core.Response toInboundResponse(jakarta.ws.rs.core.Response outboundResponse) Converts an outboundResponsebuilt as if from inside a Jakarta EE resource, e.g., something likeResponse.ok(entity).build(), into an inbound Response on whichResponse.readEntity(Class)can be called. Inbound responses are what Jakarta Clients return, and thereadEntitymethods are how client code gets the response entity.This is useful if you are testing client code, and you want to mock the response returned by an endpoint to test how the client responds to various responses, e.g., different error conditions. If you don't do this and your test attempts to read an entity from an outbound response, an IllegalStateException is thrown.
Note that in this implementation, only
Response.readEntity(Class)andResponse.readEntity(GenericType)will return the entity. The other tworeadEntitymethods that accept an array of Annotation will throw an exception.All the credit goes to Ashley Frieze for this solution. See his blog entry on this: Testing and Mocking Jersey Responses
- Parameters:
outboundResponse- the outbound response- Returns:
- a simulated inbound response
- Implementation Note:
- The implementation uses Mockito to spy the outbound response, essentially intercepting calls to
readEntity and returning the value returned by
Response.getEntity()instead.
-