Class TestClientResponse

java.lang.Object
ru.vyarus.dropwizard.guice.test.client.builder.TestClientResponse
All Implemented Interfaces:
AutoCloseable

public class TestClientResponse extends Object implements AutoCloseable
Wrapper for jersey Response with extra assertion shortcuts (jersey response object is general-purpose and this api assumed to be used for tests).

Provides the following method groups:

  • "as*" methods convert the response body (shortcuts for Response.readEntity(Class))
  • "assert*" methods to simplify response assertions. All these methods use junit 5 assertions (methods with Predicate also use assertions internally).
  • "with*" methods for manual operations with various objects (to not create additional variable in test)

Response would close only if response body was read. To indicate the importance of manual close, AutoCloseable is implemented (same as in response) and so IDEA would highlight usage without "try-with-resources". You could ignore it when the client is used with guicey test extensions, as all responses would be closed just after the test application shutdown.

Original response object is accessible with asResponse().

Since:
12.09.2025
  • Constructor Details

    • TestClientResponse

      public TestClientResponse(jakarta.ws.rs.core.Response response)
      Create a client response wrapper.
      Parameters:
      response - response object to wrap
  • Method Details

    • asString

      public String asString()
      Warning: there is no explicit check for the request success state because this method might be used to read the error body. Use assertSuccess() explicitly to make sure request was successful.
      Returns:
      response body as string
    • asList

      public <T> List<T> asList(Class<T> entityType)
      Shortcut method to avoid GenericType usage for simple lists (same as response.readEntity(new GenericType&lt;List&lt;EntityType&gt;&gt;(){})).

      Warning: there is no explicit check for the request success state because this method might be used to read the error body. Use assertSuccess() explicitly to make sure request was successful.

      Type Parameters:
      T - entity type
      Parameters:
      entityType - list entities type
      Returns:
      response body, read as list
      Throws:
      IllegalStateException - if entity was already read (e.g. by as(Class))
    • as

      public <T> T as(Class<T> entityType)
      Read response body as declared type (same as Response.readEntity(Class)). For complex types use as(GenericType).

      Warning: there is no explicit check for the request success state because this method might be used to read the error body. Use assertSuccess() explicitly to make sure request was successful.

      Type Parameters:
      T - entity type
      Parameters:
      entityType - entity type
      Returns:
      result mapped into entity
      Throws:
      IllegalStateException - if entity was already read
    • as

      public <T> T as(jakarta.ws.rs.core.GenericType<T> entityType)
      Required for types with generics.For example: .as(new GenericType<Something<Else>>(){}).

      Note: when the result is assigned to variable, there is no need to specify type - use diamond operator (new GenericType<>(){}) (but there is no way to avoid specifying generic type at all).

      For simple list cases use asList(Class)

      Warning: there is no explicit check for the request success state because this method might be used to read the error body. Use assertSuccess() explicitly to make sure request was successful.

      Type Parameters:
      T - entity type
      Parameters:
      entityType - entity type
      Returns:
      result mapped into entity
      Throws:
      IllegalStateException - if entity was already read
    • as

      public <T> T as(Function<jakarta.ws.rs.core.Response,T> converter)
      General response conversion logic. Exists for chained calls - to avoid redundant variable usage in the test.

      Warning: there is no explicit check for the request success state because this method might be used to read the error body. Use assertSuccess() explicitly to make sure request was successful.

      Type Parameters:
      T - entity type
      Parameters:
      converter - response converter.
      Returns:
      conversion result
    • asResponse

      public jakarta.ws.rs.core.Response asResponse()
      Returns:
      raw response object
    • asFile

      public Path asFile(Path tmpDir)
      Download the response file into a directory. In contrast to response.readEntity(File.class), this method preserves file name (if provided) and creates not temporary file. If file name collide with already existing file, created file name would be modified with "(index)",

      Usually it's easier to manage local temp directory in test rather than rely on global temp.

      Warning: there is no explicit check for the request success state because this method might be used to read the error body. Use assertSuccess() explicitly to make sure request was successful.

      Parameters:
      tmpDir - temporary files directory
      Returns:
      downloaded file path
    • close

      public void close()
      Close the underlying response object. This is required ONLY when the response body was not acquired by any of as* methods (excluding asResponse).

      It is not required to call it explicitly when client is used with guicey test extensions because guicey would close all used responses after test application shutdown.

      Specified by:
      close in interface AutoCloseable
    • assertResponse

      public <T> TestClientResponse assertResponse(Class<T> entityType, Predicate<T> predicate)
      Assert a mapped response with custom condition.

      For custom assertions use withResponse(Class, java.util.function.Consumer).

      Type Parameters:
      T - entity type
      Parameters:
      entityType - entity type
      predicate - assertion condition
      Returns:
      response instance for chained calls
      Throws:
      IllegalStateException - if entity was already read (e.g. by as(Class))
    • assertResponse

      public <T> TestClientResponse assertResponse(jakarta.ws.rs.core.GenericType<T> entityType, Predicate<T> predicate)
      Assert a mapped response with custom condition.

      For custom assertions use withResponse(jakarta.ws.rs.core.GenericType, java.util.function.Consumer).

      Type Parameters:
      T - entity type
      Parameters:
      entityType - entity type
      predicate - assertion condition
      Returns:
      response instance for chained calls
      Throws:
      IllegalStateException - if entity was already read (e.g. by as(Class))
    • assertResponse

      public TestClientResponse assertResponse(Predicate<jakarta.ws.rs.core.Response> consumer)
      Assert a response object (general assertion logic).

      For custom assertions use withResponse(java.util.function.Consumer).

      Parameters:
      consumer - response assertion
      Returns:
      response instance for chained calls
    • assertStatus

      public TestClientResponse assertStatus(Integer... expectedStatuses)
      Assert response status to be one of the provided statuses. Note that often it's simpler to check for status family using assertStatus(jakarta.ws.rs.core.Response.Status.Family).

      For custom assertions use withStatus(java.util.function.Consumer).

      Parameters:
      expectedStatuses - expected statuses
      Returns:
      response instance for chained calls
      See Also:
      • HttpStatus
    • assertStatus

      public TestClientResponse assertStatus(jakarta.ws.rs.core.Response.Status.Family family)
      Assert status family (family is a first digit in status code so SUCCESS family would mean any of 200, 201, 204, etc.).

      For custom assertions use withStatus(java.util.function.Consumer).

      Parameters:
      family - expected family
      Returns:
      response instance for chained calls
    • assertStatus

      public TestClientResponse assertStatus(Predicate<jakarta.ws.rs.core.Response.StatusType> predicate)
      Assert status object.

      For custom assertions use withStatus(java.util.function.Consumer).

      Parameters:
      predicate - predicate to check status object
      Returns:
      response instance for chained calls
    • assertSuccess

      public TestClientResponse assertSuccess()
      Assert response status to be 2xx.

      For custom assertions use withStatus(java.util.function.Consumer).

      Returns:
      response instance for chained calls
    • assertFail

      public TestClientResponse assertFail()
      Assert response status is not 2xx (not success). Note that redirection also falls into this category, but, as normally, the client follows redirects, then it should not be a problem.

      For custom assertions use withStatus(java.util.function.Consumer).

      Returns:
      response instance for chained calls
    • assertRedirect

      public TestClientResponse assertRedirect()
      Assert response status is 3xx. Note that by default the client follows redirects, so it's important to disable it to assert redirection correctness.

      For custom assertions use withStatus(java.util.function.Consumer).

      Returns:
      response instance for chained calls
    • assertCacheControl

      public TestClientResponse assertCacheControl(Predicate<jakarta.ws.rs.core.CacheControl> predicate)
      Assert cache control header value.

      For custom assertions use withCacheControl(java.util.function.Consumer).

      Parameters:
      predicate - assertion condition
      Returns:
      response instance for chained calls
    • assertVoidResponse

      public TestClientResponse assertVoidResponse()
      Assert the response have no body.

      For custom assertions use withResponse(java.util.function.Consumer)

      Returns:
      response instance for chained calls
      Throws:
      IllegalStateException - if entity was already read (e.g. by as(Class))
    • assertMedia

      public TestClientResponse assertMedia(jakarta.ws.rs.core.MediaType mediaType)
      Assert response media type.
      Parameters:
      mediaType - expected media type
      Returns:
      response instance for chained calls
    • assertLocale

      public TestClientResponse assertLocale(Locale locale)
      Assert response locale.
      Parameters:
      locale - expected locale
      Returns:
      response instance for chained calls
    • assertHeader

      public TestClientResponse assertHeader(String name, String value)
      Assert header value. In case of multiple headers present, its values would be concatenated with "," and the resulting string would be compared with the expected value.

      For custom assertions use withHeader(org.eclipse.jetty.http.HttpHeader, java.util.function.Consumer).

      Parameters:
      name - header name
      value - expected value
      Returns:
      response instance for chained calls
    • assertHeader

      public TestClientResponse assertHeader(String name, Predicate<String> predicate)
      Assert header value with predicate. In case of multiple headers present, its values would be concatenated with "," and the resulting string would be compared with the expected value.

      For custom assertions use withHeader(org.eclipse.jetty.http.HttpHeader, java.util.function.Consumer).

      Parameters:
      name - header name
      predicate - value predicate
      Returns:
      response instance for chained calls
    • assertHeader

      public TestClientResponse assertHeader(org.eclipse.jetty.http.HttpHeader header, String value)
      Assert header value. In case of multiple headers present, its values would be concatenated with "," and the resulting string would be compared with the expected value.

      For custom assertions use withHeader(org.eclipse.jetty.http.HttpHeader, java.util.function.Consumer).

      Parameters:
      header - header name
      value - expected value
      Returns:
      response instance for chained calls
    • assertHeader

      public TestClientResponse assertHeader(org.eclipse.jetty.http.HttpHeader header, Predicate<String> predicate)
      Assert header value with predicate. In case of multiple headers present, its values would be concatenated with "," and the resulting string would be provided to the predicate for evaluation.

      For custom assertions use withHeader(org.eclipse.jetty.http.HttpHeader, java.util.function.Consumer).

      Parameters:
      predicate - value predicate
      header - header name
      Returns:
      response instance for chained calls
    • assertCookie

      public TestClientResponse assertCookie(String name, String value)
      Assert cookie value.

      For custom assertions use withCookie(String, java.util.function.Consumer).

      Parameters:
      name - cookie name
      value - expected value
      Returns:
      response instance for chained calls
    • assertCookie

      public TestClientResponse assertCookie(String name, Predicate<jakarta.ws.rs.core.Cookie> predicate)
      Assert cookie value with a predicate.

      For custom assertions use withCookie(String, java.util.function.Consumer).

      Parameters:
      name - cookie name
      predicate - value predicate
      Returns:
      response instance for chained calls
    • withResponse

      public <T> TestClientResponse withResponse(Class<T> entityType, Consumer<T> consumer)
      Assert response body mapping and the resulted object.
      Type Parameters:
      T - entity type
      Parameters:
      entityType - entity type
      consumer - mapped result consumer
      Returns:
      response instance for chained calls
      Throws:
      IllegalStateException - if entity was already read (e.g. by as(Class))
    • withResponse

      public <T> TestClientResponse withResponse(jakarta.ws.rs.core.GenericType<T> entityType, Consumer<T> consumer)
      Assert response body mapping and the resulted object.
      Type Parameters:
      T - entity type
      Parameters:
      entityType - entity type
      consumer - mapped result consumer
      Returns:
      response instance for chained calls
      Throws:
      IllegalStateException - if entity was already read (e.g. by as(Class))
    • withResponse

      public TestClientResponse withResponse(Consumer<jakarta.ws.rs.core.Response> consumer)
      Manually examine a response object and perform manual assertions.
      Parameters:
      consumer - response object consumer
      Returns:
      response instance for chained calls
    • withStatus

      public TestClientResponse withStatus(Consumer<jakarta.ws.rs.core.Response.StatusType> consumer)
      Manually examine a status object and perform assertions.
      Parameters:
      consumer - status object consumer
      Returns:
      response instance for chained calls
    • withHeader

      public TestClientResponse withHeader(org.eclipse.jetty.http.HttpHeader header, Consumer<String> consumer)
      Manually examine header and perform assertions. In case of multiple headers present, its values would be concatenated with "," and the resulting string would be provided for manual assertions.
      Parameters:
      header - header name
      consumer - value consumer
      Returns:
      response instance for chained calls
    • withHeader

      public TestClientResponse withHeader(String name, Consumer<String> consumer)
      Manually examine header and perform assertions. In case of multiple headers present, its values would be concatenated with "," and the resulting string would be provided for manual assertions.
      Parameters:
      name - header name
      consumer - value consumer
      Returns:
      response instance for chained calls
    • withCookie

      public TestClientResponse withCookie(String name, Consumer<jakarta.ws.rs.core.Cookie> consumer)
      Manually examine cookie and perform assertions.
      Parameters:
      name - cookie name
      consumer - cookie object consumer
      Returns:
      response instance for chained calls
    • withCacheControl

      public TestClientResponse withCacheControl(Consumer<jakarta.ws.rs.core.CacheControl> consumer)
      Manually examine the cache control header and perform assertions.
      Parameters:
      consumer - cache control consumer
      Returns:
      response instance for chained calls
    • toString

      public String toString()
      Overrides:
      toString in class Object