Class MockResponse<T>

java.lang.Object
kong.unirest.core.BaseResponse<T>
kong.unirest.core.MockResponse<T>
Type Parameters:
T - the body type
All Implemented Interfaces:
HttpResponse<T>

public class MockResponse<T> extends BaseResponse<T>
A Mock Response that can be used in testing.
  • Field Details

  • Constructor Details

    • MockResponse

      public MockResponse(int status, String statusText, T body)
      Construct a mock Response
      Parameters:
      status - the status of the response
      statusText - the status text
      body - the body
    • MockResponse

      public MockResponse(int status, String statusText, T body, MockConfig config)
      Construct a mock Response
      Parameters:
      status - the status of the response
      statusText - the status text
      body - the body
      config - a mockConfig for post-processing options
  • Method Details

    • ok

      public static <T> MockResponse<T> ok(T body)
      Construct a simple successful (200 ok) response with a body
      Type Parameters:
      T - the type of body
      Parameters:
      body - the body
      Returns:
      a MockResponse;
    • bad

      public static <T> MockResponse<T> bad(T body)
      Construct a simple failed (400 bad request) response with a body
      Type Parameters:
      T - the type of body
      Parameters:
      body - the body
      Returns:
      a MockResponse;
    • of

      public static <T> MockResponse<T> of(int status, T body)
      Construct a response with a status and body. The status text is just the string of the status
      Type Parameters:
      T - the type of body
      Parameters:
      status - the status
      body - the body
      Returns:
      a MockResponse;
    • of

      public static <T> MockResponse<T> of(int status, String statusText, T body)
      Construct a response with a status and body. The status text is just the string of the status
      Type Parameters:
      T - the type of body
      Parameters:
      status - the status
      statusText - the status text
      body - the body
      Returns:
      a MockResponse;
    • config

      public MockConfig config()
      get the MockConfig for this MockResponse
      Returns:
      the config
    • getBody

      public T getBody()
      Specified by:
      getBody in interface HttpResponse<T>
      Specified by:
      getBody in class BaseResponse<T>
      Returns:
      the body
    • getRawBody

      protected String getRawBody()
      Specified by:
      getRawBody in class BaseResponse<T>
    • getParsingError

      public Optional<UnirestParsingException> getParsingError()
      Description copied from interface: HttpResponse
      If the transformation to the body failed by an exception it will be kept here
      Specified by:
      getParsingError in interface HttpResponse<T>
      Overrides:
      getParsingError in class BaseResponse<T>
      Returns:
      a possible RuntimeException. Checked exceptions are wrapped in a UnirestException
    • withHeader

      public MockResponse<T> withHeader(String key, String value)
      add a header value to the response
      Parameters:
      key - the header key
      value - the header value
      Returns:
      this MockResponse
    • failedToParse

      public MockResponse<T> failedToParse()
      Flag that there was a post-processing parsing error with the object Mapper this jams the body as a string into the parsing exception and sets a generic oops exception
      Returns:
      this MockResponse
    • failedToParse

      public MockResponse<T> failedToParse(Exception e, String originalBody)
      Flag that there was a post-processing parsing error with the object Mapper
      Parameters:
      e - the exception thrown
      originalBody - the original body before the object mapper got involved.
      Returns:
      this MockResponse
    • withConfigOptions

      public MockResponse<T> withConfigOptions(Consumer<MockConfig> c)
      Set some options on the current MockConfig.
      Parameters:
      c - a Consumer with options to set on the config
      Returns:
      this MockResponse