Package org.springframework.test.web.client.response

# Class MockRestResponseCreators

java.lang.Object
org.springframework.test.web.client.response.MockRestResponseCreators

public abstract class MockRestResponseCreators extends Object
Static factory methods to obtain a `ResponseCreator` with a fixed response.

In addition, see also the `ExecutingResponseCreator` implementation that performs actual requests to remote services.

Since:
  3.2
Author:
  Rossen Stoyanchev
See Also:
  - `ExecutingResponseCreator`

## Constructor Summary

Constructors
Constructor
Description
`MockRestResponseCreators()`

## Method Summary

Modifier and Type
Method
Description
`static DefaultResponseCreator`
`withAccepted()`
`ResponseCreator` for a 202 response (ACCEPTED).
`static DefaultResponseCreator`
`withBadGateway()`
`ResponseCreator` for a 502 response (BAD_GATEWAY).
`static DefaultResponseCreator`
`withBadRequest()`
`ResponseCreator` for a 400 response (BAD_REQUEST).
`static DefaultResponseCreator`
`withCreatedEntity(URI location)`
`ResponseCreator` for a 201 response (CREATED) with a 'Location' header.
`static ResponseCreator`
`withException(IOException ex)`
`ResponseCreator` with an internal application `IOException`.
`static DefaultResponseCreator`
`withForbiddenRequest()`
`ResponseCreator` for a 403 response (FORBIDDEN).
`static DefaultResponseCreator`
`withGatewayTimeout()`
`ResponseCreator` for a 504 response (GATEWAY_TIMEOUT).
`static DefaultResponseCreator`
`withNoContent()`
`ResponseCreator` for a 204 response (NO_CONTENT).
`static DefaultResponseCreator`
`withRawStatus(int status)`
Variant of `withStatus(HttpStatusCode)` with an integer.
`static DefaultResponseCreator`
`withRequestConflict()`
`ResponseCreator` for a 409 response (CONFLICT).
`static DefaultResponseCreator`
`withResourceNotFound()`
`ResponseCreator` for a 404 response (NOT_FOUND).
`static DefaultResponseCreator`
`withServerError()`
`ResponseCreator` for a 500 response (SERVER_ERROR).
`static DefaultResponseCreator`
`withServiceUnavailable()`
`ResponseCreator` for a 503 response (SERVICE_UNAVAILABLE).
`static DefaultResponseCreator`
`withStatus(org.springframework.http.HttpStatusCode status)`
`ResponseCreator` with a specific HTTP status.
`static DefaultResponseCreator`
`withSuccess()`
`ResponseCreator` for a 200 response (OK).
`static DefaultResponseCreator`
`withSuccess(byte[] body, org.springframework.http.MediaType contentType)`
`ResponseCreator` for a 200 response (OK) with byte[] body.
`static DefaultResponseCreator`
`withSuccess(String body, org.springframework.http.MediaType contentType)`
`ResponseCreator` for a 200 response (OK) with String body.
`static DefaultResponseCreator`
`withSuccess(org.springframework.core.io.Resource body, org.springframework.http.MediaType contentType)`
`ResponseCreator` for a 200 response (OK) content with `Resource`-based body.
`static DefaultResponseCreator`
`withTooManyRequests()`
`ResponseCreator` for a 429 ratelimited response (TOO_MANY_REQUESTS).
`static DefaultResponseCreator`
`withTooManyRequests(int retryAfter)`
`ResponseCreator` for a 429 rate-limited response (TOO_MANY_REQUESTS) with a `Retry-After` header in seconds.
`static DefaultResponseCreator`
`withUnauthorizedRequest()`
`ResponseCreator` for a 401 response (UNAUTHORIZED).

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### MockRestResponseCreators

public MockRestResponseCreators()

## Method Details

### withSuccess

public static DefaultResponseCreator withSuccess()
`ResponseCreator` for a 200 response (OK).

### withSuccess

public static DefaultResponseCreator withSuccess(String body, @Nullable org.springframework.http.MediaType contentType)
`ResponseCreator` for a 200 response (OK) with String body.

Parameters:
  `body` - the response body, a "UTF-8" string
  `contentType` - the type of the content (may be `null`)

### withSuccess

public static DefaultResponseCreator withSuccess(byte[] body, @Nullable org.springframework.http.MediaType contentType)
`ResponseCreator` for a 200 response (OK) with byte[] body.

Parameters:
  `body` - the response body
  `contentType` - the type of the content (may be `null`)

### withSuccess

public static DefaultResponseCreator withSuccess(org.springframework.core.io.Resource body, @Nullable org.springframework.http.MediaType contentType)
`ResponseCreator` for a 200 response (OK) content with `Resource`-based body.

Parameters:
  `body` - the response body
  `contentType` - the type of the content (may be `null`)

### withCreatedEntity

public static DefaultResponseCreator withCreatedEntity(URI location)
`ResponseCreator` for a 201 response (CREATED) with a 'Location' header.

Parameters:
  `location` - the value for the `Location` header

### withAccepted

public static DefaultResponseCreator withAccepted()
`ResponseCreator` for a 202 response (ACCEPTED).

Since:
  6.0

### withNoContent

public static DefaultResponseCreator withNoContent()
`ResponseCreator` for a 204 response (NO_CONTENT).

### withBadRequest

public static DefaultResponseCreator withBadRequest()
`ResponseCreator` for a 400 response (BAD_REQUEST).

### withUnauthorizedRequest

public static DefaultResponseCreator withUnauthorizedRequest()
`ResponseCreator` for a 401 response (UNAUTHORIZED).

### withForbiddenRequest

public static DefaultResponseCreator withForbiddenRequest()
`ResponseCreator` for a 403 response (FORBIDDEN).

Since:
  6.0

### withResourceNotFound

public static DefaultResponseCreator withResourceNotFound()
`ResponseCreator` for a 404 response (NOT_FOUND).

Since:
  6.0

### withRequestConflict

public static DefaultResponseCreator withRequestConflict()
`ResponseCreator` for a 409 response (CONFLICT).

Since:
  6.0

### withTooManyRequests

public static DefaultResponseCreator withTooManyRequests()
`ResponseCreator` for a 429 ratelimited response (TOO_MANY_REQUESTS).

Since:
  6.0

### withTooManyRequests

public static DefaultResponseCreator withTooManyRequests(int retryAfter)
`ResponseCreator` for a 429 rate-limited response (TOO_MANY_REQUESTS) with a `Retry-After` header in seconds.

Since:
  6.0

### withServerError

public static DefaultResponseCreator withServerError()
`ResponseCreator` for a 500 response (SERVER_ERROR).

### withBadGateway

public static DefaultResponseCreator withBadGateway()
`ResponseCreator` for a 502 response (BAD_GATEWAY).

Since:
  6.0

### withServiceUnavailable

public static DefaultResponseCreator withServiceUnavailable()
`ResponseCreator` for a 503 response (SERVICE_UNAVAILABLE).

Since:
  6.0

### withGatewayTimeout

public static DefaultResponseCreator withGatewayTimeout()
`ResponseCreator` for a 504 response (GATEWAY_TIMEOUT).

Since:
  6.0

### withStatus

public static DefaultResponseCreator withStatus(org.springframework.http.HttpStatusCode status)
`ResponseCreator` with a specific HTTP status.

Parameters:
  `status` - the response status

### withRawStatus

public static DefaultResponseCreator withRawStatus(int status)
Variant of `withStatus(HttpStatusCode)` with an integer.

Parameters:
  `status` - the response status
Since:
  5.3.17

### withException

public static ResponseCreator withException(IOException ex)
`ResponseCreator` with an internal application `IOException`.

For example, one could use this to simulate a `SocketTimeoutException`.

Parameters:
  `ex` - the `Exception` to be thrown at HTTP call time
Since:
  5.2.2