Class TestClientResponse
- All Implemented Interfaces:
AutoCloseable
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
Predicatealso 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 Summary
ConstructorsConstructorDescriptionTestClientResponse(jakarta.ws.rs.core.Response response) Create a client response wrapper. -
Method Summary
Modifier and TypeMethodDescription<T> Tas(jakarta.ws.rs.core.GenericType<T> entityType) Required for types with generics.For example:.as(new GenericType<Something<Else>>(){}).<T> TRead response body as declared type (same asResponse.readEntity(Class)).<T> TGeneral response conversion logic.Download the response file into a directory.<T> List<T>Shortcut method to avoidGenericTypeusage for simple lists (same asresponse.readEntity(new GenericType<List<EntityType>>(){})).jakarta.ws.rs.core.ResponseassertCacheControl(Predicate<jakarta.ws.rs.core.CacheControl> predicate) Assert cache control header value.assertCookie(String name, String value) Assert cookie value.assertCookie(String name, Predicate<jakarta.ws.rs.core.Cookie> predicate) Assert cookie value with a predicate.Assert response status is not 2xx (not success).assertHeader(String name, String value) Assert header value.assertHeader(String name, Predicate<String> predicate) Assert header value with predicate.assertHeader(org.eclipse.jetty.http.HttpHeader header, String value) Assert header value.assertHeader(org.eclipse.jetty.http.HttpHeader header, Predicate<String> predicate) Assert header value with predicate.assertLocale(Locale locale) Assert response locale.assertMedia(jakarta.ws.rs.core.MediaType mediaType) Assert response media type.Assert response status is 3xx.assertResponse(jakarta.ws.rs.core.GenericType<T> entityType, Predicate<T> predicate) Assert a mapped response with custom condition.assertResponse(Class<T> entityType, Predicate<T> predicate) Assert a mapped response with custom condition.assertResponse(Predicate<jakarta.ws.rs.core.Response> consumer) Assert a response object (general assertion logic).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.).assertStatus(Integer... expectedStatuses) Assert response status to be one of the provided statuses.assertStatus(Predicate<jakarta.ws.rs.core.Response.StatusType> predicate) Assert status object.Assert response status to be 2xx.Assert the response have no body.asString()Warning: there is no explicit check for the request success state because this method might be used to read the error body.voidclose()Close the underlying response object.toString()withCacheControl(Consumer<jakarta.ws.rs.core.CacheControl> consumer) Manually examine the cache control header and perform assertions.withCookie(String name, Consumer<jakarta.ws.rs.core.Cookie> consumer) Manually examine cookie and perform assertions.withHeader(String name, Consumer<String> consumer) Manually examine header and perform assertions.withHeader(org.eclipse.jetty.http.HttpHeader header, Consumer<String> consumer) Manually examine header and perform assertions.withResponse(jakarta.ws.rs.core.GenericType<T> entityType, Consumer<T> consumer) Assert response body mapping and the resulted object.withResponse(Class<T> entityType, Consumer<T> consumer) Assert response body mapping and the resulted object.withResponse(Consumer<jakarta.ws.rs.core.Response> consumer) Manually examine a response object and perform manual assertions.withStatus(Consumer<jakarta.ws.rs.core.Response.StatusType> consumer) Manually examine a status object and perform assertions.
-
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
Warning: there is no explicit check for the request success state because this method might be used to read the error body. UseassertSuccess()explicitly to make sure request was successful.- Returns:
- response body as string
-
asList
Shortcut method to avoidGenericTypeusage for simple lists (same asresponse.readEntity(new GenericType<List<EntityType>>(){})).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. byas(Class))
-
as
Read response body as declared type (same asResponse.readEntity(Class)). For complex types useas(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
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
Download the response file into a directory. In contrast toresponse.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:
closein interfaceAutoCloseable
-
assertResponse
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 typepredicate- assertion condition- Returns:
- response instance for chained calls
- Throws:
IllegalStateException- if entity was already read (e.g. byas(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 typepredicate- assertion condition- Returns:
- response instance for chained calls
- Throws:
IllegalStateException- if entity was already read (e.g. byas(Class))
-
assertResponse
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
Assert response status to be one of the provided statuses. Note that often it's simpler to check for status family usingassertStatus(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
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
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
Assert response status to be 2xx.For custom assertions use
withStatus(java.util.function.Consumer).- Returns:
- response instance for chained calls
-
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
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
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
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. byas(Class))
-
assertMedia
Assert response media type.- Parameters:
mediaType- expected media type- Returns:
- response instance for chained calls
-
assertLocale
Assert response locale.- Parameters:
locale- expected locale- Returns:
- response instance for chained calls
-
assertHeader
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 namevalue- expected value- Returns:
- response instance for chained calls
-
assertHeader
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 namepredicate- value predicate- Returns:
- response instance for chained calls
-
assertHeader
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 namevalue- 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 predicateheader- header name- Returns:
- response instance for chained calls
-
assertCookie
Assert cookie value.For custom assertions use
withCookie(String, java.util.function.Consumer).- Parameters:
name- cookie namevalue- expected value- Returns:
- response instance for chained calls
-
assertCookie
Assert cookie value with a predicate.For custom assertions use
withCookie(String, java.util.function.Consumer).- Parameters:
name- cookie namepredicate- value predicate- Returns:
- response instance for chained calls
-
withResponse
Assert response body mapping and the resulted object.- Type Parameters:
T- entity type- Parameters:
entityType- entity typeconsumer- mapped result consumer- Returns:
- response instance for chained calls
- Throws:
IllegalStateException- if entity was already read (e.g. byas(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 typeconsumer- mapped result consumer- Returns:
- response instance for chained calls
- Throws:
IllegalStateException- if entity was already read (e.g. byas(Class))
-
withResponse
Manually examine a response object and perform manual assertions.- Parameters:
consumer- response object consumer- Returns:
- response instance for chained calls
-
withStatus
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 nameconsumer- value consumer- Returns:
- response instance for chained calls
-
withHeader
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 nameconsumer- value consumer- Returns:
- response instance for chained calls
-
withCookie
Manually examine cookie and perform assertions.- Parameters:
name- cookie nameconsumer- cookie object consumer- Returns:
- response instance for chained calls
-
withCacheControl
Manually examine the cache control header and perform assertions.- Parameters:
consumer- cache control consumer- Returns:
- response instance for chained calls
-
toString
-