Class TestClientRequestBuilder
Request could be executed:
- With direct response mapping:
as(Class) - As void
asVoid()when response body is not important, just the success status - With success validation
expectSuccess(Integer...)(it will also throw exception according to response status) - With fail validation:
expectFailure(Integer...) - With redirect validation:
expectRedirect(Integer...) - Without status checks:
invoke()
In cases, when direct result mapping is not requested, a special response wrapper would be returned, supporting assertions in a builder manner.
Builder does not hide jerse WebTarget and
Invocation.Builder objects: they could be configured manually with:
configurePath(Function) and configureRequest(Consumer).
- Since:
- 12.09.2025
-
Constructor Summary
ConstructorsConstructorDescriptionTestClientRequestBuilder(jakarta.ws.rs.client.WebTarget target, String httpMethod, jakarta.ws.rs.client.Entity<?> body, @Nullable TestRequestConfig defaults) Construct new request builder. -
Method Summary
Modifier and TypeMethodDescriptionaccept(jakarta.ws.rs.core.MediaType... accept) Configure request Accept header (Invocation.Builder.accept(jakarta.ws.rs.core.MediaType...)).Configure request Accept header (Invocation.Builder.accept(String...)).acceptEncoding(String... encodings) Configure request Accept-Encoding header (Invocation.Builder.acceptEncoding(String...)).acceptLanguage(String... language) Configure request Accept-Language header (Invocation.Builder.acceptLanguage(String...)).acceptLanguage(Locale... language) Configure request Accept-Language header (Invocation.Builder.acceptLanguage(java.util.Locale...)).<T> Tas(@Nullable jakarta.ws.rs.core.GenericType<T> type) Invoke request and expect successful status (2xx).<T> TInvoke request and expect successful status (2xx).assertRequest(Consumer<RequestTracker> action) AssertWebTargetandInvocation.Builderobjects configurations.asString()Shortcut toas(Class).voidasVoid()Invoke request and expect successful status (2xx).cacheControl(jakarta.ws.rs.core.CacheControl cacheControl) Configure request Cache-Control header (Invocation.Builder.cacheControl(CacheControl)).cacheControl(String cacheControl) Configure request Cache-Control header (Invocation.Builder.cacheControl(CacheControl)).configurePath(Function<jakarta.ws.rs.client.WebTarget, jakarta.ws.rs.client.WebTarget> modifier) Manual jersey target object configuration.configureRequest(Consumer<jakarta.ws.rs.client.Invocation.Builder> modifier) Manual jersey request object configuration.cookie(jakarta.ws.rs.core.Cookie cookie) Configure request cookie (Invocation.Builder.cookie(jakarta.ws.rs.core.Cookie).Configure request cookie (Invocation.Builder.cookie(String, String).Configure multiple cookies at once.debug()Enable console reporting for configured default values and applied request customizations.Shortcut for jersey trace requesting: jersey should return trace as X-Jersey-Tracing-NNN headers.enableJerseyTrace(org.glassfish.jersey.message.internal.TracingLogger.Level level) enableJerseyTrace()with custom trace level.expectFailure(Integer... statuses) Invoke a request with status validation.expectRedirect(Integer... statuses) Invoke a request with status validation.expectSuccess(Integer... statuses) Invoke a request with status validation.Configure request header (Invocation.Builder.header(String, Object)).Configure request header (Invocation.Builder.header(String, Object)).Configure multiple headers at once.invoke()Invoke request without status validation (same as jerseyInvocation.invoke()).matrixParam(String name, Object value) Configure request matrix parameter (WebTarget.matrixParam(String, Object...)).matrixParams(Map<String, ?> params) Configure multiple matrix params at once.In theory, jersey should avoid body mapping when void response requested (builder.readEntity(Void.class)), but jersey tries to map it and fils.Disable redirects following to validate redirection correctness (or redirection fact).Configure request path parameter (WebTarget.resolveTemplateFromEncoded(String, Object)).pathParams(Map<String, ?> params) Configure multiple path params at once.properties(Map<String, ?> props) Configure multiple properties at once.Jersey client property configuration (JerseyClient.property(String, Object)).queryParam(String name, Object value) Configure request query parameter (WebTarget.queryParam(String, Object...)).queryParams(Map<String, ?> params) Configure multiple query params at once.Jersey client extension registration (JerseyClient.register(Class)).Jersey client extension registration (JerseyClient.register(Object)).toString()
-
Constructor Details
-
TestClientRequestBuilder
public TestClientRequestBuilder(jakarta.ws.rs.client.WebTarget target, String httpMethod, jakarta.ws.rs.client.Entity<?> body, @Nullable TestRequestConfig defaults) Construct new request builder.- Parameters:
target- request targethttpMethod- request methodbody- request entity (body)defaults- default request configuration (common headers, cookies, path params, etc)
-
-
Method Details
-
configurePath
public TestClientRequestBuilder configurePath(Function<jakarta.ws.rs.client.WebTarget, jakarta.ws.rs.client.WebTarget> modifier) Manual jersey target object configuration.- Parameters:
modifier- function, applying target configuration- Returns:
- builder instance for chained calls
-
configureRequest
public TestClientRequestBuilder configureRequest(Consumer<jakarta.ws.rs.client.Invocation.Builder> modifier) Manual jersey request object configuration.- Parameters:
modifier- consumer, applying request configuration- Returns:
- builder instance for chained calls
-
notFollowRedirects
Disable redirects following to validate redirection correctness (or redirection fact).Option is enabled automatically when
expectRedirect(Integer...)is used.- Returns:
- builder instance for chained calls
-
noBodyMappingForVoid
In theory, jersey should avoid body mapping when void response requested (builder.readEntity(Void.class)), but jersey tries to map it and fils. This shortcut registers a special body mapper which would completely ignore the response body.Enabled automatically when
asVoid()is used.- Returns:
- builder instance for chained calls
-
accept
Configure request Accept header (Invocation.Builder.accept(jakarta.ws.rs.core.MediaType...)).Multiple calls override previous value.
- Parameters:
accept- media types required for response- Returns:
- builder instance for chained calls
- See Also:
-
MediaType
-
accept
Configure request Accept header (Invocation.Builder.accept(String...)).Multiple calls override previous value.
- Parameters:
accept- media types required for response- Returns:
- builder instance for chained calls
- See Also:
-
MediaType
-
acceptLanguage
Configure request Accept-Language header (Invocation.Builder.acceptLanguage(java.util.Locale...)).Multiple calls override previous value.
- Parameters:
language- languages to accept- Returns:
- builder instance for chained calls
-
acceptLanguage
Configure request Accept-Language header (Invocation.Builder.acceptLanguage(String...)).Multiple calls override previous value.
- Parameters:
language- languages to accept- Returns:
- builder instance for chained calls
-
acceptEncoding
Configure request Accept-Encoding header (Invocation.Builder.acceptEncoding(String...)).Multiple calls override previous value.
- Parameters:
encodings- encodings to accept- Returns:
- builder instance for chained calls
-
queryParam
Configure request query parameter (WebTarget.queryParam(String, Object...)).Note: jersey api supports multiple query parameters with the same name (in this case multiple parameters added). The same result could be achieved by passing list or array into this api.
Multiple calls with the same name override the previous value (the only way to specify multiple values is using list or array as value).
- Parameters:
name- query parameter namevalue- parameter value (list or array for multiple values)- Returns:
- builder instance for chained calls
-
queryParams
Configure multiple query params at once.Note: jersey api supports multiple query parameters with the same name (in this case multiple parameters added). The same result could be achieved by passing list or array into this api.
Multiple calls append to previous configurations (parameters with the same name are overridden)
- Parameters:
params- query params map- Returns:
- builder instance for chained calls
-
matrixParam
Configure request matrix parameter (WebTarget.matrixParam(String, Object...)).Note: jersey api supports multiple matrix parameters with the same name (in this case multiple parameters added). The same result could be achieved by passing list or array into this api.
Multiple calls with the same name override the previous value (the only way to specify multiple values is using list or array as value).
- Parameters:
name- matrix parameter namevalue- parameter value (list or array for multiple values)- Returns:
- builder instance for chained calls
-
matrixParams
Configure multiple matrix params at once.Note: jersey api supports multiple matrix parameters with the same name (in this case multiple parameters added). The same result could be achieved by passing list or array into this api.
Multiple calls append to previous configurations (parameters with the same name are overridden)
- Parameters:
params- query params map- Returns:
- builder instance for chained calls
-
pathParam
Configure request path parameter (WebTarget.resolveTemplateFromEncoded(String, Object)).Note: parameter value assumed to be encoded to be able to specify matrix parameters (in the middle of the path):
pathParam("var", "/path;var=1;val=2").- Parameters:
name- parameter namevalue- parameter value- Returns:
- builder instance for chained calls
-
pathParams
Configure multiple path params at once.Multiple calls append to previous configurations (parameters with the same name are overridden)
- Parameters:
params- parameters map- Returns:
- builder instance for chained calls
-
header
Configure request header (Invocation.Builder.header(String, Object)).Multiple calls override previous value.
- Parameters:
name- header namevalue- header value- Returns:
- builder instance for chained calls
-
header
Configure request header (Invocation.Builder.header(String, Object)).Multiple calls override previous value.
- Parameters:
name- header namevalue- header value- Returns:
- builder instance for chained calls
-
headers
Configure multiple headers at once.Multiple calls append to previous configurations (headers with the same name are overridden)
- Parameters:
headers- headers map- Returns:
- builder instance for chained calls
-
cookie
Configure request cookie (Invocation.Builder.cookie(String, String).Multiple calls override previous value.
- Parameters:
name- cookie namevalue- cookie value- Returns:
- builder instance for chained calls
-
cookie
Configure request cookie (Invocation.Builder.cookie(jakarta.ws.rs.core.Cookie).Use cookie builder:
new NewCookie.Builder(name).value(value).build().Multiple calls override previous value.
- Parameters:
cookie- cookie value- Returns:
- builder instance for chained calls
- See Also:
-
NewCookie
-
cookies
Configure multiple cookies at once.Multiple calls append to previous configurations (cookies with the same name are overridden).
- Parameters:
cookies- cookie map- Returns:
- builder instance for chained calls
-
property
Jersey client property configuration (JerseyClient.property(String, Object)).Multiple calls override previous value.
- Parameters:
name- property namevalue- property value- Returns:
- builder instance for chained calls
- See Also:
-
ClientProperties
-
properties
Configure multiple properties at once.Multiple calls append to previous configurations (properties with the same name are overridden).
- Parameters:
props- properties map- Returns:
- builder instance for chained calls
-
register
Jersey client extension registration (JerseyClient.register(Class)). Could be useful, for example, to register customMessageBodyReader.Multiple registrations of the same class will be ignored (extensions tracked by type).
- Parameters:
type- extension class- Returns:
- builder instance for chained calls
-
register
Jersey client extension registration (JerseyClient.register(Object)). Could be useful, for example, to register customMessageBodyReader(as instance).When called with different instances of the same class: only the latest registration will be used (extensions tracked by type).
- Parameters:
extension- extensions instance- Returns:
- builder instance for chained calls
-
cacheControl
Configure request Cache-Control header (Invocation.Builder.cacheControl(CacheControl)).Multiple calls override previous value.
- Parameters:
cacheControl- cache control header value (string) to apply to request.- Returns:
- builder instance for chained calls
-
cacheControl
Configure request Cache-Control header (Invocation.Builder.cacheControl(CacheControl)).Multiple calls override previous value.
- Parameters:
cacheControl- cache control settings to apply to request.- Returns:
- builder instance for chained calls
-
debug
Enable console reporting for configured default values and applied request customizations.- Returns:
- builder instance for chained calls
-
enableJerseyTrace
Shortcut for jersey trace requesting: jersey should return trace as X-Jersey-Tracing-NNN headers.WARNING: Tracing support MUST be enabled on server with:
environment.jersey().property(ServerProperties.TRACING, TracingConfig.ON_DEMAND.name());This is assumed to be used for remote apis.
- Returns:
- builder instance for chained calls
-
enableJerseyTrace
public TestClientRequestBuilder enableJerseyTrace(org.glassfish.jersey.message.internal.TracingLogger.Level level) enableJerseyTrace()with custom trace level.- Parameters:
level- trace level- Returns:
- builder instance for chained calls
-
assertRequest
AssertWebTargetandInvocation.Builderobjects configurations. Could be used to verify request correctness.Implicitly enables
debug(), so all applied configurations would be printed to console first and then assertions applied.Assertions executed just before request execution.
- Parameters:
action- assertion action- Returns:
- builder instance for chanined calls
-
asVoid
public void asVoid()Invoke request and expect successful status (2xx). Response body is ignored.In case of not successful request, exception would be thrown according to response status.
Note: this option would implicitly enable
noBodyMappingForVoid().Under
StubRestit will bypass original exception (when no exception mappers registered). -
as
Invoke request and expect successful status (2xx). Response body is mapped to the specified type. This is essentially the same as jersey shortcut methods likeresponse.get(SomeEntity.class).In case of not successful request, exception would be thrown according to response status.
Under
StubRestit will bypass original exception (when no exception mappers registered).- Type Parameters:
T- response type- Parameters:
type- response type- Returns:
- mapped response instance
-
as
public <T> T as(@Nullable jakarta.ws.rs.core.GenericType<T> type) Invoke request and expect successful status (2xx). Response body is mapped to the specified type. This is essentially the same as jersey shortcut methods likeresponse.get(new GenericType<SomeEntity>(){}.In case of not successful request, exception would be thrown according to response status.
Under
StubRestit will bypass original exception (when no exception mappers registered).- Type Parameters:
T- response type- Parameters:
type- response type- Returns:
- mapped response instance
-
asString
Shortcut toas(Class).- Returns:
- response body as string
-
invoke
Invoke request without status validation (same as jerseyInvocation.invoke()). For automatic status validation seeexpectSuccess(Integer...),expectFailure(Integer...)andexpectRedirect(Integer...).Under
StubRestit will bypass original exception (when no exception mappers registered).Note: IDE may highlight that method must be used under 'try-with-resources` (because the underlying response must be correctly closed), but you can ignore it, because all responses are closed automatically.
- Returns:
- response wrapper object with additional assertions
-
expectSuccess
Invoke a request with status validation. It will throw an exception for non 2xx response (same asas(Class)). Will throw assertion error if response status does not match.Under
StubRestit will bypass original exception (when no exception mappers registered).Note: IDE may highlight that method must be used under 'try-with-resources` (because the underlying response must be correctly closed), but you can ignore it, because all responses are closed automatically.
- Parameters:
statuses- (optional) statuses to match (when exact status validation is important)- Returns:
- response wrapper object with additional assertions
-
expectFailure
Invoke a request with status validation. Will throw assertion error if response status is success or does not match one of the provided statuses.Suitable for testing error response (or when exception mapped by some exception mapper).
Under
StubRestit will bypass original exception (when no exception mappers registered).Note: IDE may highlight that method must be used under 'try-with-resources` (because the underlying response must be correctly closed), but you can ignore it, because all responses are closed automatically.
- Parameters:
statuses- (optional) statuses to match (when exact status validation is important)- Returns:
- response wrapper object with additional assertions
-
expectRedirect
Invoke a request with status validation. Will throw assertion error if response status is not redirect or does not match one of provided statuses.Note: implicitly enables
notFollowRedirects().Under
StubRestit will bypass original exception (when no exception mappers registered).Note: IDE may highlight that method must be used under 'try-with-resources` (because the underlying response must be correctly closed), but you can ignore it, because all responses are closed automatically.
- Parameters:
statuses- (optional) statuses to match (when exact status validation is important)- Returns:
- response wrapper object with additional assertions
-
getConfig
- Returns:
- request configuration
-
toString
-