Class JakartaRestTestHelpers

java.lang.Object
org.kiwiproject.beta.test.jersey.ws.rs.JakartaRestTestHelpers

@Beta public final class JakartaRestTestHelpers extends Object
Test utilities for Jakarta RESTful Web Services.
  • Method Summary

    Modifier and Type
    Method
    Description
    static jakarta.ws.rs.core.Response
    toInboundResponse(jakarta.ws.rs.core.Response outboundResponse)
    Converts an outbound Response built as if from inside a Jakarta EE resource, e.g., something like Response.ok(entity).build(), into an inbound Response on which Response.readEntity(Class) can be called.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • toInboundResponse

      public static jakarta.ws.rs.core.Response toInboundResponse(jakarta.ws.rs.core.Response outboundResponse)
      Converts an outbound Response built as if from inside a Jakarta EE resource, e.g., something like Response.ok(entity).build(), into an inbound Response on which Response.readEntity(Class) can be called. Inbound responses are what Jakarta Clients return, and the readEntity methods 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) and Response.readEntity(GenericType) will return the entity. The other two readEntity methods 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.