Skip to main content

NG Assertions

RestResponse.assertThat() returns a ResponseAssertion (package org.apache.juneau.rest.client.assertion) for fluent checks. Failures throw AssertionError, so they work directly in JUnit tests.

try (var resp = client.get("/users/42").run()) {
resp.assertThat()
.isOk()
.header("Content-Type").contains("json").and()
.header("ETag").isPresent().and()
.body().contains("\"id\":42").and();
}

ResponseAssertion​

MethodChecks
statusCode()Returns an integer assertion on the status.
isOk()Status is 2xx.
isClientError() / isServerError()Status is 4xx / 5xx.
header(name)Switches to a ResponseHeaderAssertion.
body()Switches to a ResponseBodyAssertion.
and()Returns to the parent assertion.

ResponseHeaderAssertion​

isPresent(), isAbsent(), equals(String), contains(String), startsWith(String), integerEquals(int), and().

ResponseBodyAssertion​

equals(String), contains(String), startsWith(String), isEmpty(), isNotEmpty(), and().

Body assertions read the body, so they consume it. Use them instead of, not in addition to, body().as(...) on the same response.

Assertions versus exceptions​

  • assertThat() is for tests: it throws AssertionError.
  • assertOk() / assertStatus(int) on the response are for application code: they throw RestCallException, which you can catch and inspect with getStatusCode().