public interface ValidatableResponseOptions<T extends ValidatableResponseOptions<T,R>,R extends ResponseBody<R> & ResponseOptions<R>>
Usage example:
get("/lotto").then().body("lotto.lottoId", is(5));
Modifier and Type | Method and Description |
---|---|
T |
and()
Syntactic sugar, e.g.
|
default T |
appendRoot(String pathToAppend)
Deprecated.
Use
appendRootPath(String) instead |
default T |
appendRoot(String pathToAppend,
List<Argument> arguments)
Deprecated.
Use
appendRootPath(String, List) instead |
T |
appendRootPath(String pathToAppend)
Append the given path to the root path of the response body so that you don't need to write the entire path for each expectation.
|
T |
appendRootPath(String pathToAppend,
List<Argument> arguments)
Append the given path to the root path with arguments supplied of the response body so that you don't need to write the entire path for each expectation.
|
T |
assertThat()
Syntactic sugar, e.g.
|
T |
body(List<Argument> arguments,
org.hamcrest.Matcher matcher,
Object... additionalKeyMatcherPairs)
This as special kind of expectation that is mainly useful when you've specified a root path with an argument placeholder.
|
T |
body(List<Argument> arguments,
ResponseAwareMatcher<R> responseAwareMatcher)
Compare a path in the response body to something available in the response using arguments when root path is used.
|
T |
body(org.hamcrest.Matcher<?> matcher,
org.hamcrest.Matcher<?>... additionalMatchers)
Validate that the response body conforms to one or more Hamcrest matchers.
|
T |
body(String path,
List<Argument> arguments,
org.hamcrest.Matcher matcher,
Object... additionalKeyMatcherPairs)
Same as
body(String, org.hamcrest.Matcher, Object...) expect that you can pass arguments to the key. |
T |
body(String path,
List<Argument> arguments,
ResponseAwareMatcher<R> responseAwareMatcher)
Compare a path in the response body to something available in the response using arguments.
|
T |
body(String path,
org.hamcrest.Matcher<?> matcher,
Object... additionalKeyMatcherPairs)
Validate that the JSON or XML response body conforms to one or more Hamcrest matchers.
|
T |
body(String path,
ResponseAwareMatcher<R> responseAwareMatcher)
Compare a path in the response body to something available in the response, for example another path.
|
T |
contentType(ContentType contentType)
Set the response content type to be
contentType . |
T |
contentType(org.hamcrest.Matcher<? super String> contentType)
Validate the response content type to be
contentType . |
T |
contentType(String contentType)
Set the response content type to be
contentType . |
T |
cookie(String cookieName)
Validate that a cookie exist in the response, regardless of value (it may have no value at all).
|
T |
cookie(String cookieName,
DetailedCookieMatcher detailedCookieMatcher)
Validate that a detailed response cookie matches the supplied cookie name and hamcrest matcher (see
DetailedCookieMatcher ). |
T |
cookie(String cookieName,
org.hamcrest.Matcher<?> expectedValueMatcher)
Validate that a response cookie matches the supplied cookie name and hamcrest matcher.
|
T |
cookie(String cookieName,
Object expectedValue)
Validate that a response cookie matches the supplied name and value.
|
T |
cookies(Map<String,?> expectedCookies)
Validate that response cookies matches those specified in a Map.
|
T |
cookies(String firstExpectedCookieName,
Object firstExpectedCookieValue,
Object... expectedCookieNameValuePairs)
Validate that response cookies matches the supplied cookie names and values.
|
T |
defaultParser(Parser parser)
Register a default predefined parser that will be used if no other parser (registered or pre-defined) matches the response
content-type.
|
default T |
detachRoot(String pathToDetach)
Deprecated.
Use
detachRootPath(String) instead |
T |
detachRootPath(String pathToDetach)
Detach the given path from the root path.
|
ExtractableResponse<R> |
extract()
Extract values from the response or return the response instance itself.
|
<V> T |
header(String headerName,
java.util.function.Function<String,V> mappingFunction,
org.hamcrest.Matcher<? super V> expectedValueMatcher)
Expect that a response header matches the supplied header name and hamcrest matcher using a mapping function.
|
T |
header(String headerName,
org.hamcrest.Matcher<?> expectedValueMatcher)
Validate that a response header matches the supplied header name and hamcrest matcher.
|
T |
header(String headerName,
ResponseAwareMatcher<R> expectedValueMatcher)
Compare a header in the response to something else available in the response.
|
T |
header(String headerName,
String expectedValue)
Validate that a response header matches the supplied name and value.
|
T |
headers(Map<String,?> expectedHeaders)
Validate that response headers matches those specified in a Map.
|
T |
headers(String firstExpectedHeaderName,
Object firstExpectedHeaderValue,
Object... expectedHeaders)
Validate that response headers matches the supplied headers and values.
|
ValidatableResponseLogSpec<T,R> |
log()
Returns the
ValidatableResponseLogSpec that allows you to log different parts of the Response . |
T |
noRoot()
Deprecated.
Use
noRootPath() instead |
T |
noRootPath()
Reset the root path of the response body so that you don't need to write the entire path for each expectation.
|
T |
parser(String contentType,
Parser parser)
Register a content-type to be parsed using a predefined parser.
|
T |
root(String rootPath)
Deprecated.
Use
rootPath(String) instead |
T |
root(String rootPath,
List<Argument> arguments)
Deprecated.
Use
rootPath(String, List) instead |
T |
rootPath(String rootPath)
Set the root path of the response body so that you don't need to write the entire path for each expectation.
|
T |
rootPath(String rootPath,
List<Argument> arguments)
Set the root path with arguments of the response body so that you don't need to write the entire path for each expectation.
|
T |
spec(ResponseSpecification responseSpecificationToMerge)
Validate that the response matches an entire specification.
|
T |
statusCode(int expectedStatusCode)
Validate that the response status code matches an integer.
|
T |
statusCode(org.hamcrest.Matcher<? super Integer> expectedStatusCode)
Validate that the response status code matches the given Hamcrest matcher.
|
T |
statusLine(org.hamcrest.Matcher<? super String> expectedStatusLine)
Validate that the response status line matches the given Hamcrest matcher.
|
T |
statusLine(String expectedStatusLine)
Validate that the response status line matches the given String.
|
T |
time(org.hamcrest.Matcher<Long> matcher)
Validate that the response time (in milliseconds) matches the supplied
matcher . |
T |
time(org.hamcrest.Matcher<Long> matcher,
TimeUnit timeUnit)
Validate that the response time matches the supplied
matcher and time unit. |
T |
using()
Syntactic sugar, e.g.
|
T body(String path, List<Argument> arguments, org.hamcrest.Matcher matcher, Object... additionalKeyMatcherPairs)
body(String, org.hamcrest.Matcher, Object...)
expect that you can pass arguments to the key. This
is useful in situations where you have e.g. pre-defined variables that constitutes the key:
String someSubPath = "else"; int index = 1; get("/x").then().body("something.%s[%d]", withArgs(someSubPath, index), equalTo("some value")). ..or if you have complex root paths and don't wish to duplicate the path for small variations:
get("/x").then(). root("filters.filterConfig[%d].filterConfigGroups.find { it.name == 'Gold' }.includes"). body(withArgs(0), hasItem("first")). body(withArgs(1), hasItem("second")). ..The key and arguments follows the standard formatting syntax of Java.
Note that withArgs
can be statically imported from the io.restassured.RestAssured
class.
path
- The body patharguments
- The arguments to apply to the keymatcher
- The hamcrest matcher that must response body must match.additionalKeyMatcherPairs
- Optionally additional hamcrest matchers that must return true
.body(String, org.hamcrest.Matcher, Object...)
T body(List<Argument> arguments, org.hamcrest.Matcher matcher, Object... additionalKeyMatcherPairs)
get(..).then(). root("x.%s"). // Root path with a placeholder body(withArgs("firstName"), equalTo(..)). body(withArgs("lastName"), equalTo(..)).Note that this is the same as doing:
get(..).then(). root("x.%s"). // Root path with a placeholder content(withArgs("firstName"), equalTo(..)). content(withArgs("lastName"), equalTo(..)).
arguments
- The arguments to apply to the root path.matcher
- The hamcrest matcher that must response body must match.additionalKeyMatcherPairs
- Optionally additional hamcrest matchers that must return true
.body(String, org.hamcrest.Matcher, Object...)
T body(List<Argument> arguments, ResponseAwareMatcher<R> responseAwareMatcher)
{ "data" : { "user1" : { "userId" : "my-id1", "href" : "http://localhost:8080/my-id1" }, "user2" : { "userId" : "my-id2", "href" : "http://localhost:8080/my-id2" }, } }you can then verify the href using:
when(). get("/x"). then(). root("data.%s"). body(withArgs("user1"), new ResponseAwareMatcherNote that you can also use some of the predefined methods in() { public Matcher> matcher(Response response) { return equalTo("http://localhost:8080/" + response.path("userId")); } });
RestAssuredMatchers
.responseAwareMatcher
- The ResponseAwareMatcher
that creates the Hamcrest matcher.body(String, ResponseAwareMatcher)
,
RestAssuredMatchers.endsWithPath(String)
,
RestAssuredMatchers.startsWithPath(String)
,
RestAssuredMatchers.containsPath(String)
,
RestAssuredMatchers.equalToPath(String)
T statusCode(org.hamcrest.Matcher<? super Integer> expectedStatusCode)
get("/something").then().assertThat().statusCode(equalTo(200));
expectedStatusCode
- The expected status code matcher.T statusCode(int expectedStatusCode)
get("/something").then().assertThat().statusCode(200);This is the same as:
get("/something").then().assertThat().statusCode(equalTo(200));
expectedStatusCode
- The expected status code.T statusLine(org.hamcrest.Matcher<? super String> expectedStatusLine)
expect().statusLine(equalTo("No Content")).when().get("/something");
expectedStatusLine
- The expected status line matcher.T statusLine(String expectedStatusLine)
expect().statusLine("No Content").when().get("/something");This is the same as:
expect().statusLine(equalTo("No Content")).when().get("/something");
expectedStatusLine
- The expected status line.T headers(Map<String,?> expectedHeaders)
E.g. expect that the response of the GET request to "/something" contains header headerName1=headerValue1 and headerName2=headerValue2:
Map expectedHeaders = new HashMap(); expectedHeaders.put("headerName1", "headerValue1")); expectedHeaders.put("headerName2", "headerValue2"); get("/something").then().assertThat().headers(expectedHeaders);
You can also use Hamcrest matchers:
Map expectedHeaders = new HashMap(); expectedHeaders.put("Content-Type", containsString("charset=UTF-8")); expectedHeaders.put("Content-Length", "160"); get("/something").then().assertThat().headers(expectedHeaders);
expectedHeaders
- The Map of expected response headersT headers(String firstExpectedHeaderName, Object firstExpectedHeaderValue, Object... expectedHeaders)
E.g. expect that the response of the GET request to "/something" contains header Pragma=no-cache and Content-Encoding=gzip:
get("/something").then().assertThat().headers("Pragma", "no-cache", "Content-Encoding", "gzip");
You can also use Hamcrest matchers:
get("/something").then().assertThat().headers("Content-Type", containsString("application/json"), "Pragma", equalsTo("no-cache"));and you can even mix string matching and hamcrest matching:
get("/something").then().assertThat().headers("Content-Type", containsString("application/json"), "Pragma", "no-cache");
firstExpectedHeaderName
- The name of the first headerfirstExpectedHeaderValue
- The value of the first headerexpectedHeaders
- A list of expected "header name" - "header value" pairs.T header(String headerName, org.hamcrest.Matcher<?> expectedValueMatcher)
E.g. expect that the response of the GET request to "/something" contains header Pragma=no-cache:
get("/something").then().assertThat().header("Pragma", containsString("no"));
You can also expect several headers:
get("/something").then().assertThat().header("Pragma", equalsTo("no-cache")).and().header("Content-Encoding", containsString("zip"));Also take a look at
headers(String, Object, Object...)
)} for a short version of passing multiple headers.
headerName
- The name of the expected headerexpectedValueMatcher
- The Hamcrest matcher that must conform to the valueT header(String headerName, ResponseAwareMatcher<R> expectedValueMatcher)
For example imagine that a POST to resource "/x" returns "201 Created" and sets a Location header
that should end with "/x/{id}" where {id}
is present in the response body:
{ "id" : 5 }To verify that the Location header ends with "/x/{id}" you can do like this:
given().param("id", 1).body(..).post("/x").then().assertThat().header("Location", response -> endsWith("/x/" + response.path("id")));
headerName
- The name of the expected headerexpectedValueMatcher
- The Hamcrest matcher that must conform to the value<V> T header(String headerName, java.util.function.Function<String,V> mappingFunction, org.hamcrest.Matcher<? super V> expectedValueMatcher)
E.g. expect that the response of the GET request to "/something" contains header Content-Length: 500 and you want to validate that the length must always be less than 600:
when(). get("/something"). then(). header("Content-Length", Integer::parseInt, lessThan(600));
headerName
- The name of the expected headermappingFunction
- Map the header to another value type before exposing it to the Hamcrest matcherexpectedValueMatcher
- The Hamcrest matcher that must conform to the valueT header(String headerName, String expectedValue)
E.g. expect that the response of the GET request to "/something" contains header Pragma=no-cache:
get("/something").then().assertThat().header("Pragma", "no-cache");
You can also expect several headers:
get("/something").then().assertThat().header("Pragma", "no-cache").and().header("Content-Encoding", "gzip");Also take a look at
headers(String, Object, Object...)
for a short version of passing multiple headers.
headerName
- The name of the expected headerexpectedValue
- The value of the expected headerT cookies(Map<String,?> expectedCookies)
E.g. expect that the response of the GET request to "/something" contains cookies cookieName1=cookieValue1 and cookieName2=cookieValue2:
Map expectedCookies = new HashMap(); expectedCookies.put("cookieName1", "cookieValue1")); expectedCookies.put("cookieName2", "cookieValue2"); get("/something").then().assertThat().cookies(expectedCookies);
You can also use Hamcrest matchers:
Map expectedCookies = new HashMap(); expectedCookies.put("cookieName1", containsString("Value1")); expectedCookies.put("cookieName2", "cookieValue2"); get("/something").then().assertThat().cookies(expectedCookies);
expectedCookies
- A Map of expected response cookiesT cookie(String cookieName)
cookieName
- the cookie to validate that it existsT cookies(String firstExpectedCookieName, Object firstExpectedCookieValue, Object... expectedCookieNameValuePairs)
E.g. expect that the response of the GET request to "/something" contains cookies cookieName1=cookieValue1 and cookieName2=cookieValue2:
get("/something").then().assertThat().cookies("cookieName1", "cookieValue1", "cookieName2", "cookieValue2");
You can also use Hamcrest matchers:
get("/something").then().assertThat().cookies("cookieName1", containsString("Value1"), "cookieName2", equalsTo("cookieValue2"));and you can even mix string matching and hamcrest matching:
get("/something").then().assertThat().cookies("cookieName1", containsString("Value1"), "cookieName2", "cookieValue2");
firstExpectedCookieName
- The name of the first cookiefirstExpectedCookieValue
- The value of the first cookieexpectedCookieNameValuePairs
- A list of expected "cookie name" - "cookie value" pairs.T cookie(String cookieName, org.hamcrest.Matcher<?> expectedValueMatcher)
E.g. expect that the response of the GET request to "/something" contain cookie cookieName1=cookieValue1
get("/something").then().assertThat().cookie("cookieName1", containsString("Value1"));
You can also expect several cookies:
get("/something").then().assertThat().cookie("cookieName1", equalsTo("cookieValue1")).and().cookie("cookieName2", containsString("Value2"));Also take a look at
cookies(String, Object, Object...)
for a short version of passing multiple cookies.
cookieName
- The name of the expected cookieexpectedValueMatcher
- The Hamcrest matcher that must conform to the valueT cookie(String cookieName, DetailedCookieMatcher detailedCookieMatcher)
DetailedCookieMatcher
).
E.g. expect that the response of the GET request to "/something" contain cookie cookieName1=cookieValue1
get("/something").then().assertThat() .detailedCookie("cookieName1", detailedCookie().value("cookieValue1").secured(true));
You can also expect several cookies:
get("/something").then().assertThat().detailedCookie("cookieName1", detailedCookie().value("cookieValue1").secured(true)) .and().detailedCookie("cookieName2", detailedCookie().value("cookieValue2").secured(false));
cookieName
- The name of the expected cookiedetailedCookieMatcher
- The Hamcrest matcher that must conform to the cookieT cookie(String cookieName, Object expectedValue)
E.g. expect that the response of the GET request to "/something" contain cookie cookieName1=cookieValue1:
get("/something").then().assertThat().cookie("cookieName1", "cookieValue1");
You can also expect several cookies:
get("/something").then().assertThat().cookie("cookieName1", "cookieValue1").and().cookie("cookieName2", "cookieValue2");Also take a look at
cookies(String, Object, Object...)
for a short version of passing multiple cookies.
cookieName
- The name of the expected cookieexpectedValue
- The value of the expected cookieT rootPath(String rootPath)
get(..).then(). body("x.y.firstName", is(..)). body("x.y.lastName", is(..)). body("x.y.age", is(..)). body("x.y.gender", is(..)).you can use a root path and do:
get(..).then(). rootPath("x.y"). body("firstName", is(..)). body("lastName", is(..)). body("age", is(..)). body("gender", is(..));Note that this method is exactly the same as
root(String)
.rootPath
- The root path to use.T rootPath(String rootPath, List<Argument> arguments)
root(String, java.util.List)
.rootPath
- The root path to use.arguments
- A list of arguments. The path and arguments follows the standard formatting syntax of Java.rootPath(String)
@Deprecated T root(String rootPath, List<Argument> arguments)
rootPath(String, List)
insteadrootPath(String, java.util.List)
.rootPath
- The root path to use.arguments
- The list of substitution arguments. The path and arguments follows the standard formatting syntax of Java..rootPath(String)
@Deprecated T root(String rootPath)
rootPath(String)
insteadget(..).then(). body("x.y.firstName", is(..)). body("x.y.lastName", is(..)). body("x.y.age", is(..)). body("x.y.gender", is(..));you can use a root and do:
get(..).then(). root("x.y"). body("firstName", is(..)). body("lastName", is(..)). body("age", is(..)). body("gender", is(..)).Note that this method is exactly the same as
rootPath(String)
but slightly shorter.rootPath
- The root path to use.@Deprecated T noRoot()
noRootPath()
insteadget(..).then(). root("x.y"). body("firstName", is(..)). body("lastName", is(..)). noRoot() body("z.something1", is(..)). body("w.something2", is(..));This is the same as calling
rootPath("")
but more expressive.
Note that this method is exactly the same as noRootPath()
but slightly shorter.root(String)
T noRootPath()
get(..).then(). rootPath("x.y"). body("firstName", is(..)). body("lastName", is(..)). noRootPath() body("z.something1", is(..)). body("w.something2", is(..)).This is the same as calling
rootPath("")
but more expressive.
Note that this method is exactly the same as noRoot()
but slightly more expressive.rootPath(String)
T appendRootPath(String pathToAppend)
get(..).then(). root("x.y"). body("age", is(..)). body("gender", is(..)). body("name.firstName", is(..)). body("name.lastName", is(..));you can use a append root and do:
get(..).then(). root("x.y"). body("age", is(..)). body("gender", is(..)). appendRoot("name"). body("firstName", is(..)). body("lastName", is(..));
pathToAppend
- The root path to append.@Deprecated default T appendRoot(String pathToAppend)
appendRootPath(String)
insteadpathToAppend
- The root path to append.appendRootPath(String)
T appendRootPath(String pathToAppend, List<Argument> arguments)
String namePath = "name"; get(..).then(). root("x.y"). body("age", is(..)). body("gender", is(..)). body(namePath + "first", is(..)). body(namePath + "last", is(..)).you can use a append root and do:
String namePath = "name"; get(..).then(). root("x.y"). body("age", is(..)). body("gender", is(..)). appendRoot("%s", withArgs(namePath)). body("first", is(..)). body("last", is(..)).
pathToAppend
- The root path to append. The path and arguments follows the standard formatting syntax of Java.@Deprecated default T appendRoot(String pathToAppend, List<Argument> arguments)
appendRootPath(String, List)
insteadpathToAppend
- The root path to append.appendRootPath(String, List)
T detachRootPath(String pathToDetach)
when(). get(..); then(). root("x.y"). body("age", is(..)). body("gender", is(..)). root("x"). body("firstName", is(..)). body("lastName", is(..)).you can use a append root and do:
when(). get(..); then(). root("x.y"). body("age", is(..)). body("gender", is(..)). detachRoot("y"). body("firstName", is(..)). body("lastName", is(..)).
pathToDetach
- The root path to detach.@Deprecated default T detachRoot(String pathToDetach)
detachRootPath(String)
insteadpathToDetach
- The root path to detach.detachRootPath(String)
T contentType(ContentType contentType)
contentType
.
Note that this will affect the way the response is decoded.
E,g. if you can't use JSON/XML matching (see e.g. body(String, org.hamcrest.Matcher, Object...)
) if you specify a
content-type of "text/plain". If you don't specify the response content type REST Assured will automatically try to
figure out which content type to use.
contentType
- The content type of the response.T contentType(String contentType)
contentType
.
Note that this will affect the way the response is decoded.
E,g. if you can't use JSON/XML matching (see e.g. body(String, org.hamcrest.Matcher, Object...)
) if you specify a
content-type of "text/plain". If you don't specify the response content type REST Assured will automatically try to
figure out which content type to use.
contentType
- The content type of the response.T contentType(org.hamcrest.Matcher<? super String> contentType)
contentType
.contentType
- The expected content type of the response.T body(org.hamcrest.Matcher<?> matcher, org.hamcrest.Matcher<?>... additionalMatchers)
// Validate that the response body (content) contains the string "winning-numbers" get("/lotto").then().assertThat().body(containsString("winning-numbers")); // Validate that the response body (content) contains the string "winning-numbers" and "winners" get("/lotto").then().assertThat().body(containsString("winning-numbers"), containsString("winners"));
matcher
- The hamcrest matcher that must response body must match.additionalMatchers
- Optionally additional hamcrest matchers that must return true
.T body(String path, List<Argument> arguments, ResponseAwareMatcher<R> responseAwareMatcher)
{ "userId" : "my-id", "my.href" : "http://localhost:8080/my-id" }you can then verify the href using:
get("/x").then().body("%s.href", withArgs("my"), new ResponseAwareMatcherNote that you can also use some of the predefined methods in() { public Matcher> matcher(Response response) { return equalTo("http://localhost:8080/" + response.path("userId")); } });
RestAssuredMatchers
.path
- The body pathresponseAwareMatcher
- The ResponseAwareMatcher
that creates the Hamcrest matcher.body(String, ResponseAwareMatcher)
,
RestAssuredMatchers.endsWithPath(String)
,
RestAssuredMatchers.startsWithPath(String)
,
RestAssuredMatchers.containsPath(String)
,
RestAssuredMatchers.equalToPath(String)
T body(String path, ResponseAwareMatcher<R> responseAwareMatcher)
{ "userId" : "my-id", "href" : "http://localhost:8080/my-id" }you can then verify the href using:
get("/x").then().body("href", new ResponseAwareMatcherNote that you can also use some of the predefined methods in() { public Matcher> matcher(Response response) { return equalTo("http://localhost:8080/" + response.path("userId")); } });
RestAssuredMatchers
.path
- The body pathresponseAwareMatcher
- The ResponseAwareMatcher
that creates the Hamcrest matcher.body(String, java.util.List, ResponseAwareMatcher)
,
RestAssuredMatchers.endsWithPath(String)
,
RestAssuredMatchers.startsWithPath(String)
,
RestAssuredMatchers.containsPath(String)
,
RestAssuredMatchers.equalToPath(String)
T body(String path, org.hamcrest.Matcher<?> matcher, Object... additionalKeyMatcherPairs)
Assume that a GET request to "/lotto" returns a JSON response containing:
{ "lotto":{ "lottoId":5, "winning-numbers":[2,45,34,23,7,5,3], "winners":[{ "winnerId":23, "numbers":[2,45,34,23,3,5] },{ "winnerId":54, "numbers":[52,3,12,11,18,22] }] }}You can verify that the lottoId is equal to 5 like this:
get("/lotto").then().assertThat().body("lotto.lottoId", equalTo(5));You can also verify that e.g. one of the the winning numbers is 45.
get("/lotto").then().assertThat().body("lotto.winning-numbers", hasItem(45));Or both at the same time:
get("/lotto").then().assertThat().body("lotto.lottoId", equalTo(5)).and().body("lotto.winning-numbers", hasItem(45));or a slightly short version:
get("/lotto").then().assertThat().body("lotto.lottoId", equalTo(5), "lotto.winning-numbers", hasItem(45));
Assume that a GET request to "/xml" returns a XML response containing:
<greeting> <firstName>John</firstName> <lastName>Doe</lastName> </greeting>You can now verify that the firstName is equal to "John" like this:
get("/xml").then().assertThat().body("greeting.firstName", equalTo("John"));To verify both the first name and last name you can do like this:
get("/xml").then().assertThat().body("greeting.firstName", equalTo("John")).and().body("greeting.lastName", equalTo("Doe"));Or the slightly shorter version of:
get("/xml").then().assertThat().body("greeting.firstName", equalTo("John"), "greeting.lastName", equalTo("Doe"));
Note that if the response body type is not of type application/xml or application/json you cannot use this verification.
The only difference between the content
and body
methods are of syntactic nature.
path
- The body pathmatcher
- The hamcrest matcher that must response body must match.additionalKeyMatcherPairs
- Optionally additional hamcrest matchers that must return true
.T and()
get("/something").then().assertThat().body(containsString("OK")).and().body(containsString("something else"));is that same as:
get("/something").then().assertThat().body(containsString("OK")).body(containsString("something else"));
T using()
get("/something").then().using().defaultParser(JSON).assertThat().body(containsString("OK")).and().body(containsString("something else"));is that same as:
get("/something").then().defaultParser(JSON).body(containsString("OK")).body(containsString("something else"));
T assertThat()
get("/something").then().assertThat().body(containsString("OK")).and().body(containsString("something else"));is that same as:
get("/something").then().body(containsString("OK")).body(containsString("something else"));
T spec(ResponseSpecification responseSpecificationToMerge)
ResponseSpecification responseSpec = new ResponseSpecBuilder().expectStatusCode(200).build(); get("/something").then() spec(responseSpec). body("x.y.z", equalTo("something"));This is useful when you want to reuse multiple different expectations in several tests. The specification passed to this method is merged with the current specification. Note that the supplied specification can overwrite data in the current specification. The following settings are overwritten:
responseSpecificationToMerge
- The specification to merge with.T parser(String contentType, Parser parser)
get("/x").then().assertThat().body("document.child", equalsTo("something")Since application/custom is not registered to be processed by the XML parser by default you need to explicitly tell REST Assured to use this parser before making the request:
get("/x").then().parser("application/custom", Parser.XML).assertThat(). ..;You can also specify by it for every response by using:
RestAssured.registerParser("application/custom", Parser.XML);
contentType
- The content-type to registerparser
- The parser to use when verifying the response.T defaultParser(Parser parser)
get("/x").then().using().defaultParser(Parser.JSON).assertThat(). ..;You can also specify it for every response by using:
RestAssured.defaultParser(Parser.JSON);
parser
- The parser to use when verifying the response if no other parser is found for the response content-type.ExtractableResponse<R> extract()
title
returns the following JSON
{ "title" : "My Title", "_links": { "self": { "href": "/title" }, "next": { "href": "/title?page=2" } } }and you want to validate that content type is equal to
JSON
and the title is equal to My Title
but you also want to extract the link to the "next" title to use that in a subsequent request. This is how:
String nextTitleLink = given(). param("param_name", "param_value"). when(). get("/title"). then(). contentType(JSON). body("title", equalTo("My Title")). extract(). path("_links.next.href"); get(nextTitleLink). ..
ExtractableResponse
.ValidatableResponseLogSpec<T,R> log()
ValidatableResponseLogSpec
that allows you to log different parts of the Response
.
This is mainly useful for debug purposes when writing your tests. IT time(org.hamcrest.Matcher<Long> matcher)
matcher
. For example:
when(). get("/something"). then(). time(lessThan(2000));where
lessThan
is a Hamcrest matcherValidatableResponse
instance.T time(org.hamcrest.Matcher<Long> matcher, TimeUnit timeUnit)
matcher
and time unit. For example:
when(). get("/something"). then(). time(lessThan(2), TimeUnit.SECONDS);where
lessThan
is a Hamcrest matcherValidatableResponse
instance.Copyright © 2010–2019. All rights reserved.