public class ResponseSpecificationImpl extends Object implements FilterableResponseSpecification, groovy.lang.GroovyObject
| Modifier and Type | Class and Description |
|---|---|
class |
ResponseSpecificationImpl.HamcrestAssertionClosure |
| Constructor and Description |
|---|
ResponseSpecificationImpl(String bodyRootPath,
Object responseContentType,
ResponseSpecification defaultSpec,
ResponseParserRegistrar rpr,
RestAssuredConfig config,
LogRepository logRepository) |
ResponseSpecificationImpl(String bodyRootPath,
Object responseContentType,
ResponseSpecification defaultSpec,
ResponseParserRegistrar rpr,
RestAssuredConfig config,
Response response,
LogRepository logRepository) |
| Modifier and Type | Method and Description |
|---|---|
ResponseSpecification |
and()
Syntactic sugar, e.g.
|
ResponseSpecification |
appendRoot(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.
|
ResponseSpecification |
appendRoot(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.
|
ResponseSpecification |
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.
|
ResponseSpecification |
body(org.hamcrest.Matcher matcher,
org.hamcrest.Matcher... additionalMatchers)
Expect that the response body conforms to one or more Hamcrest matchers.
|
ResponseSpecification |
body(String key,
List<Argument> arguments,
org.hamcrest.Matcher matcher,
Object... additionalKeyMatcherPairs)
Same as
ResponseSpecification.body(String, org.hamcrest.Matcher, Object...) expect that you can pass arguments to the key. |
ResponseSpecification |
body(String key,
org.hamcrest.Matcher matcher,
Object... additionalKeyMatcherPairs)
Expect that the JSON or XML response body conforms to one or more Hamcrest matchers.
|
ResponseSpecification |
content(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.
|
ResponseSpecification |
content(org.hamcrest.Matcher matcher,
org.hamcrest.Matcher... additionalMatchers)
Expect that the response content conforms to one or more Hamcrest matchers.
|
ResponseSpecification |
content(String key,
List<Argument> arguments,
org.hamcrest.Matcher matcher,
Object... additionalKeyMatcherPairs)
Same as
ResponseSpecification.body(String, java.util.List, org.hamcrest.Matcher, Object...) expect that you can pass arguments to the path. |
ResponseSpecification |
content(String key,
org.hamcrest.Matcher matcher,
Object... additionalKeyMatcherPairs)
Expect that the JSON or XML response content conforms to one or more Hamcrest matchers.
|
ResponseSpecification |
contentType(ContentType contentType)
Set the response content type to be
contentType. |
ResponseSpecification |
contentType(org.hamcrest.Matcher<? super String> contentType)
Expect the response content type to be
contentType. |
ResponseSpecification |
contentType(String contentType)
Set the response content type to be
contentType. |
ResponseSpecification |
cookie(String cookieName)
Expect that a cookie exist in the response, regardless of value (it may have no value at all).
|
ResponseSpecification |
cookie(String cookieName,
org.hamcrest.Matcher expectedValueMatcher)
Expect that a response cookie matches the supplied cookie name and hamcrest matcher.
|
ResponseSpecification |
cookie(String cookieName,
Object expectedValue)
Expect that a response cookie matches the supplied name and value.
|
ResponseSpecification |
cookies(Map expectedCookies)
Expect that response cookies matches those specified in a Map.
|
ResponseSpecification |
cookies(String firstExpectedCookieName,
Object firstExpectedCookieValue,
Object... expectedCookieNameValuePairs)
Expect that response cookies matches the supplied cookie names and values.
|
ResponseSpecification |
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.
|
Response |
delete()
Perform a DELETE request to the statically configured path (by default
http://localhost:8080). |
Response |
delete(String path,
Map pathParams)
Perform a DELETE request to a
path. |
Response |
delete(String path,
Object... pathParams)
Perform a DELETE request to a
path. |
Response |
delete(URI uri)
Perform a DELETE request to a
uri. |
Response |
delete(URL url)
Perform a DELETE request to a
url. |
ResponseSpecification |
detachRoot(String pathToDetach)
Detach the given path from the root path.
|
ResponseSpecification |
expect()
Syntactic sugar, e.g.
|
Response |
get()
Perform a GET request to the statically configured path (by default
http://localhost:8080). |
Response |
get(String path,
Map pathParams)
Perform a GET request to a
path. |
Response |
get(String path,
Object... pathParams)
Perform a GET request to a
path. |
Response |
get(URI uri)
Perform a GET request to a
uri. |
Response |
get(URL url)
Perform a GET request to a
url. |
RestAssuredConfig |
getConfig() |
LogRepository |
getLogRepository() |
groovy.lang.MetaClass |
getMetaClass() |
Object |
getProperty(String property) |
String |
getResponseContentType() |
String |
getRootPath() |
ResponseParserRegistrar |
getRpr() |
org.hamcrest.Matcher<Integer> |
getStatusCode() |
org.hamcrest.Matcher<String> |
getStatusLine() |
RequestSpecification |
given()
Returns the request com.jayway.restassured.specification so that you can define the properties of the request.
|
boolean |
hasAssertionsDefined() |
boolean |
hasBodyAssertionsDefined() |
boolean |
hasCookieAssertions() |
boolean |
hasHeaderAssertions() |
Response |
head()
Perform a HEAD request to the statically configured path (by default
http://localhost:8080). |
Response |
head(String path,
Map pathParams)
Perform a HEAD request to a
path. |
Response |
head(String path,
Object... pathParams)
Perform a HEAD request to a
path. |
Response |
head(URI uri)
Perform a HEAD request to a
uri. |
Response |
head(URL url)
Perform a HEAD request to a
url. |
ResponseSpecification |
header(String headerName,
org.hamcrest.Matcher expectedValueMatcher)
Expect that a response header matches the supplied header name and hamcrest matcher.
|
ResponseSpecification |
header(String headerName,
String expectedValue)
Expect that a response header matches the supplied name and value.
|
ResponseSpecification |
headers(Map expectedHeaders)
Expect that response headers matches those specified in a Map.
|
ResponseSpecification |
headers(String firstExpectedHeaderName,
Object firstExpectedHeaderValue,
Object... expectedHeaders)
Expect that response headers matches the supplied headers and values.
|
Object |
invokeMethod(String method,
Object arguments) |
ResponseLogSpecification |
log()
Returns the
ResponseLogSpecification that allows you to log different parts of the ResponseSpecification. |
ResponseSpecification |
noRoot()
Reset the root path of the response body so that you don't need to write the entire path for each expectation.
|
ResponseSpecification |
noRootPath()
Reset the root path of the response body so that you don't need to write the entire path for each expectation.
|
Response |
options()
Perform a OPTIONS request to the statically configured path (by default
http://localhost:8080). |
Response |
options(String path,
Map pathParams)
Perform a OPTIONS request to a
path. |
Response |
options(String path,
Object... pathParams)
Perform a OPTIONS request to a
path. |
Response |
options(URI uri)
Perform a OPTIONS request to a
uri. |
Response |
options(URL url)
Perform a OPTIONS request to a
url. |
ResponseSpecification |
parser(String contentType,
Parser parser)
Register a content-type to be parsed using a predefined parser.
|
Response |
patch()
Perform a PATCH request to the statically configured path (by default
http://localhost:8080). |
Response |
patch(String path,
Map pathParams)
Perform a PATCH request to a
path. |
Response |
patch(String path,
Object... pathParams)
Perform a PATCH request to a
path. |
Response |
patch(URI uri)
Perform a PATCH request to a
uri. |
Response |
patch(URL url)
Perform a PATCH request to a
url. |
Response |
post()
Perform a POST request to the statically configured path (by default
http://localhost:8080). |
Response |
post(String path,
Map pathParams)
Perform a POST request to a
path. |
Response |
post(String path,
Object... pathParams)
Perform a POST request to a
path. |
Response |
post(URI uri)
Perform a POST request to a
uri. |
Response |
post(URL url)
Perform a POST request to a
url. |
Response |
put()
Perform a PUT request to the statically configured path (by default
http://localhost:8080). |
Response |
put(String path,
Map pathParams)
Perform a PUT request to a
path. |
Response |
put(String path,
Object... pathParams)
Perform a PUT request to a
path. |
Response |
put(URI uri)
Perform a PUT request to a
uri. |
Response |
put(URL url)
Perform a PUT request to a
url. |
RequestSpecification |
request()
Returns the request com.jayway.restassured.specification so that you can define the properties of the request.
|
ResponseSpecification |
response()
Syntactic sugar, e.g.
|
ResponseSpecification |
root(String rootPath)
Set the root path of the response body so that you don't need to write the entire path for each expectation.
|
ResponseSpecification |
root(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.
|
ResponseSpecification |
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.
|
ResponseSpecification |
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.
|
void |
setConfig(RestAssuredConfig value) |
void |
setMetaClass(groovy.lang.MetaClass mc) |
void |
setProperty(String property,
Object value) |
void |
setRpr(ResponseParserRegistrar value) |
ResponseSpecification |
spec(ResponseSpecification responseSpecificationToMerge)
Expect that the response matches an entire specification.
|
ResponseSpecification |
specification(ResponseSpecification responseSpecificationToMerge)
Expect that the response matches an entire specification.
|
ResponseSpecification |
statusCode(int expectedStatusCode)
Expect that the response status code matches an integer.
|
ResponseSpecification |
statusCode(org.hamcrest.Matcher<? super Integer> expectedStatusCode)
Expect that the response status code matches the given Hamcrest matcher.
|
ResponseSpecification |
statusLine(org.hamcrest.Matcher<? super String> expectedStatusLine)
Expect that the response status line matches the given Hamcrest matcher.
|
ResponseSpecification |
statusLine(String expectedStatusLine)
Expect that the response status line matches the given String.
|
ResponseSpecification |
that()
Syntactic sugar, e.g.
|
ResponseSpecification |
then()
Syntactic sugar, e.g.
|
void |
throwIllegalStateExceptionIfRootPathIsNotDefined(String description) |
Response |
validate(Response response)
Validates the specified response against this ResponseSpecification
|
ResponseSpecification |
when()
Syntactic sugar, e.g.
|
RequestSpecification |
with()
Returns the request com.jayway.restassured.specification so that you can define the properties of the request.
|
public ResponseSpecificationImpl(String bodyRootPath, Object responseContentType, ResponseSpecification defaultSpec, ResponseParserRegistrar rpr, RestAssuredConfig config, LogRepository logRepository)
public ResponseSpecificationImpl(String bodyRootPath, Object responseContentType, ResponseSpecification defaultSpec, ResponseParserRegistrar rpr, RestAssuredConfig config, Response response, LogRepository logRepository)
public groovy.lang.MetaClass getMetaClass()
getMetaClass in interface groovy.lang.GroovyObjectpublic void setMetaClass(groovy.lang.MetaClass mc)
setMetaClass in interface groovy.lang.GroovyObjectpublic Object invokeMethod(String method, Object arguments)
invokeMethod in interface groovy.lang.GroovyObjectpublic Object getProperty(String property)
getProperty in interface groovy.lang.GroovyObjectpublic void setProperty(String property, Object value)
setProperty in interface groovy.lang.GroovyObjectpublic ResponseParserRegistrar getRpr()
public void setRpr(ResponseParserRegistrar value)
public RestAssuredConfig getConfig()
public void setConfig(RestAssuredConfig value)
public ResponseSpecification content(List<Argument> arguments, org.hamcrest.Matcher matcher, Object... additionalKeyMatcherPairs)
ResponseSpecification
expect().
root("x.%s"). // Root path with a placeholder
content(withArgs("firstName"), equalTo(..)).
content(withArgs("lastName"), equalTo(..)).
when().
get(..);
Note that this is the same as doing:
expect().
root("x.%s"). // Root path with a placeholder
content(withArgs("firstName"), equalTo(..)).
content(withArgs("lastName"), equalTo(..)).
when().
get(..);
Note that this method is the same as ResponseSpecification.body(java.util.List, org.hamcrest.Matcher, Object...) but with a method name.
content in interface ResponseSpecificationarguments - 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.ResponseSpecification.body(String, org.hamcrest.Matcher, Object...)public Response validate(Response response)
ResponseSpecificationvalidate in interface ResponseSpecificationresponse - The response to validatepublic ResponseSpecification content(org.hamcrest.Matcher matcher, org.hamcrest.Matcher... additionalMatchers)
ResponseSpecification
// Expect that the response content (body) contains the string "winning-numbers"
expect().content(containsString("winning-numbers")).when().get("/lotto");
// Expect that the response content (body) contains the string "winning-numbers" and "winners"
expect().content(containsString("winning-numbers"), containsString("winners")).when().get("/lotto");
content in interface ResponseSpecificationmatcher - The hamcrest matcher that must response content must match.additionalMatchers - Optionally additional hamcrest matchers that must return true.public ResponseSpecification content(String key, org.hamcrest.Matcher matcher, Object... additionalKeyMatcherPairs)
ResponseSpecificationAssume 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:
expect().content("lotto.lottoId", equalTo(5)).when().get("/lotto");
You can also verify that e.g. one of the the winning numbers is 45.
expect().content("lotto.winning-numbers", hasItem(45)).when().get("/lotto");
Or both at the same time:
expect().content("lotto.lottoId", equalTo(5)).and().content("lotto.winning-numbers", hasItem(45)).when().get("/lotto");
or a slightly short version:
expect().content("lotto.lottoId", equalTo(5), "lotto.winning-numbers", hasItem(45)).when().get("/lotto");
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:
expect().content("greeting.firstName", equalTo("John")).when().get("/xml");
To verify both the first name and last name you can do like this:
expect().content("greeting.firstName", equalTo("John")).and().content("greeting.lastName", equalTo("Doe")).when().get("/xml");
Or the slightly shorter version of:
expect().content("greeting.firstName", equalTo("John"), "greeting.lastName", equalTo("Doe")).when().get("/xml");
Note that if the response content 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.
content in interface ResponseSpecificationmatcher - The hamcrest matcher that must response content must match.additionalKeyMatcherPairs - Optionally additional hamcrest matchers that must return true.public ResponseSpecification statusCode(org.hamcrest.Matcher<? super Integer> expectedStatusCode)
ResponseSpecification
expect().statusCode(equalTo(200)).when().get("/something");
statusCode in interface ResponseSpecificationexpectedStatusCode - The expected status code matcher.public ResponseSpecification statusCode(int expectedStatusCode)
ResponseSpecification
expect().statusCode(200).when().get("/something");
This is the same as:
expect().statusCode(equalTo(200)).when().get("/something");
statusCode in interface ResponseSpecificationexpectedStatusCode - The expected status code.public ResponseSpecification statusLine(org.hamcrest.Matcher<? super String> expectedStatusLine)
ResponseSpecification
expect().statusLine(equalTo("No Content")).when().get("/something");
statusLine in interface ResponseSpecificationexpectedStatusLine - The expected status line matcher.public ResponseSpecification headers(Map expectedHeaders)
ResponseSpecificationE.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");
expect().response().headers(expectedHeaders).when().get("/something");
You can also use Hamcrest matchers:
Map expectedHeaders = new HashMap();
expectedHeaders.put("Content-Type", containsString("charset=UTF-8"));
expectedHeaders.put("Content-Length", "160");
expect().headers(expectedHeaders).when().get("/something");
headers in interface ResponseSpecificationexpectedHeaders - The Map of expected response headerspublic ResponseSpecification headers(String firstExpectedHeaderName, Object firstExpectedHeaderValue, Object... expectedHeaders)
ResponseSpecificationE.g. expect that the response of the GET request to "/something" contains header Pragma=no-cache and Content-Encoding=gzip:
expect().headers("Pragma", "no-cache", "Content-Encoding", "gzip").when().get("/something");
You can also use Hamcrest matchers:
expect().response().headers("Content-Type", containsString("application/json"), "Pragma", equalsTo("no-cache")).when().get("/something");
and you can even mix string matching and hamcrest matching:
expect().headers("Content-Type", containsString("application/json"), "Pragma", "no-cache").when().get("/something");
headers in interface ResponseSpecificationfirstExpectedHeaderName - The name of the first headerfirstExpectedHeaderValue - The value of the first headerexpectedHeaders - A list of expected "header name" - "header value" pairs.public ResponseSpecification header(String headerName, org.hamcrest.Matcher expectedValueMatcher)
ResponseSpecificationE.g. expect that the response of the GET request to "/something" contains header Pragma=no-cache:
expect().header("Pragma", containsString("no")).when().get("/something");
You can also expect several headers:
expect().header("Pragma", equalsTo("no-cache")),and().header("Content-Encoding", containsString("zip")).when().get("/something");
Also take a look at ResponseSpecification.headers(String, Object, Object...) )} for a short version of passing multiple headers.
header in interface ResponseSpecificationheaderName - The name of the expected headerexpectedValueMatcher - The Hamcrest matcher that must conform to the valuepublic ResponseSpecification header(String headerName, String expectedValue)
ResponseSpecificationE.g. expect that the response of the GET request to "/something" contains header Pragma=no-cache:
expect().header("Pragma", "no-cache").when().get("/something");
You can also expect several headers:
expect().header("Pragma", "no-cache"),and().header("Content-Encoding", "gzip").when().get("/something");
Also take a look at ResponseSpecification.headers(String, Object, Object...) for a short version of passing multiple headers.
header in interface ResponseSpecificationheaderName - The name of the expected headerexpectedValue - The value of the expected headerpublic ResponseSpecification cookies(Map expectedCookies)
ResponseSpecificationE.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");
expect().response().cookies(expectedCookies).when().get("/something");
You can also use Hamcrest matchers:
Map expectedCookies = new HashMap();
expectedCookies.put("cookieName1", containsString("Value1"));
expectedCookies.put("cookieName2", "cookieValue2");
expect().cookies(expectedCookies).when().get("/something");
cookies in interface ResponseSpecificationexpectedCookies - A Map of expected response cookiespublic ResponseSpecification cookies(String firstExpectedCookieName, Object firstExpectedCookieValue, Object... expectedCookieNameValuePairs)
ResponseSpecificationE.g. expect that the response of the GET request to "/something" contains cookies cookieName1=cookieValue1 and cookieName2=cookieValue2:
expect().cookies("cookieName1", "cookieValue1", "cookieName2", "cookieValue2").when().get("/something");
You can also use Hamcrest matchers:
expect().response().cookies("cookieName1", containsString("Value1"), "cookieName2", equalsTo("cookieValue2")).when().get("/something");
and you can even mix string matching and hamcrest matching:
expect().cookies("cookieName1", containsString("Value1"), "cookieName2", "cookieValue2").when().get("/something");
cookies in interface ResponseSpecificationfirstExpectedCookieName - The name of the first cookiefirstExpectedCookieValue - The value of the first cookieexpectedCookieNameValuePairs - A list of expected "cookie name" - "cookie value" pairs.public ResponseSpecification cookie(String cookieName, org.hamcrest.Matcher expectedValueMatcher)
ResponseSpecificationE.g. expect that the response of the GET request to "/something" contain cookie cookieName1=cookieValue1
expect().cookie("cookieName1", containsString("Value1")).when().get("/something");
You can also expect several cookies:
expect().cookie("cookieName1", equalsTo("cookieValue1")),and().cookie("cookieName2", containsString("Value2")).when().get("/something");
Also take a look at ResponseSpecification.cookies(String, Object, Object...) for a short version of passing multiple cookies.
cookie in interface ResponseSpecificationcookieName - The name of the expected cookieexpectedValueMatcher - The Hamcrest matcher that must conform to the valuepublic ResponseSpecification cookie(String cookieName)
ResponseSpecificationcookie in interface ResponseSpecificationcookieName - the cookie to validate that it existspublic ResponseSpecification cookie(String cookieName, Object expectedValue)
ResponseSpecificationE.g. expect that the response of the GET request to "/something" contain cookie cookieName1=cookieValue1:
expect().cookie("cookieName1", "cookieValue1").when().get("/something");
You can also expect several cookies:
expect().cookie("cookieName1", "cookieValue1"),and().cookie("cookieName2", "cookieValue2").when().get("/something");
Also take a look at ResponseSpecification.cookies(String, Object, Object...) for a short version of passing multiple cookies.
cookie in interface ResponseSpecificationcookieName - The name of the expected cookieexpectedValue - The value of the expected cookiepublic ResponseSpecification spec(ResponseSpecification responseSpecificationToMerge)
ResponseSpecification
ResponseSpecification responseSpec = new ResponseSpecBuilder().expectStatusCode(200).build();
expect().
spec(responseSpec).
body("x.y.z", equalTo("something")).
when().
get("/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:
ResponseSpecification.specification(ResponseSpecification) but the name is a bit shorter.spec in interface ResponseSpecificationresponseSpecificationToMerge - The specification to merge with.public ResponseSpecification specification(ResponseSpecification responseSpecificationToMerge)
ResponseSpecification
ResponseSpecification responseSpec = new ResponseSpecBuilder().expectStatusCode(200).build();
expect().
specification(responseSpec).
body("x.y.z", equalTo("something")).
when().
get("/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:
ResponseSpecification.spec(ResponseSpecification) but the name is a bit longer.specification in interface ResponseSpecificationresponseSpecificationToMerge - The specification to merge with.public ResponseSpecification statusLine(String expectedStatusLine)
ResponseSpecification
expect().statusLine("No Content").when().get("/something");
This is the same as:
expect().statusLine(equalTo("No Content")).when().get("/something");
statusLine in interface ResponseSpecificationexpectedStatusLine - The expected status line.public ResponseSpecification body(org.hamcrest.Matcher matcher, org.hamcrest.Matcher... additionalMatchers)
ResponseSpecification
// Expect that the response body (content) contains the string "winning-numbers"
expect().body(containsString("winning-numbers")).when().get("/lotto");
// Expect that the response body (content) contains the string "winning-numbers" and "winners"
expect().body(containsString("winning-numbers"), containsString("winners")).when().get("/lotto");
body in interface ResponseSpecificationmatcher - The hamcrest matcher that must response body must match.additionalMatchers - Optionally additional hamcrest matchers that must return true.public ResponseSpecification body(String key, org.hamcrest.Matcher matcher, Object... additionalKeyMatcherPairs)
ResponseSpecificationAssume 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:
expect().body("lotto.lottoId", equalTo(5)).when().get("/lotto");
You can also verify that e.g. one of the the winning numbers is 45.
expect().body("lotto.winning-numbers", hasItem(45)).when().get("/lotto");
Or both at the same time:
expect().body("lotto.lottoId", equalTo(5)).and().body("lotto.winning-numbers", hasItem(45)).when().get("/lotto");
or a slightly short version:
expect().body("lotto.lottoId", equalTo(5), "lotto.winning-numbers", hasItem(45)).when().get("/lotto");
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:
expect().body("greeting.firstName", equalTo("John")).when().get("/xml");
To verify both the first name and last name you can do like this:
expect().body("greeting.firstName", equalTo("John")).and().body("greeting.lastName", equalTo("Doe")).when().get("/xml");
Or the slightly shorter version of:
expect().body("greeting.firstName", equalTo("John"), "greeting.lastName", equalTo("Doe")).when().get("/xml");
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.
body in interface ResponseSpecificationkey - The body pathmatcher - The hamcrest matcher that must response body must match.additionalKeyMatcherPairs - Optionally additional hamcrest matchers that must return true.public ResponseSpecification body(String key, List<Argument> arguments, org.hamcrest.Matcher matcher, Object... additionalKeyMatcherPairs)
ResponseSpecificationResponseSpecification.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;
expect().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:
expect().
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 com.jayway.restassured.RestAssured class.
body in interface ResponseSpecificationkey - The body keyarguments - 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.ResponseSpecification.body(String, org.hamcrest.Matcher, Object...)public ResponseSpecification body(List<Argument> arguments, org.hamcrest.Matcher matcher, Object... additionalKeyMatcherPairs)
ResponseSpecification
expect().
root("x.%s"). // Root path with a placeholder
body(withArgs("firstName"), equalTo(..)).
body(withArgs("lastName"), equalTo(..)).
when().
get(..);
Note that this is the same as doing:
expect().
root("x.%s"). // Root path with a placeholder
body(withArgs("firstName"), equalTo(..)).
body(withArgs("lastName"), equalTo(..)).
when().
get(..);
Note that this method is the same as ResponseSpecification.content(java.util.List, org.hamcrest.Matcher, Object...) but with a method name.
body in interface ResponseSpecificationarguments - 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.ResponseSpecification.body(String, org.hamcrest.Matcher, Object...)public ResponseSpecification content(String key, List<Argument> arguments, org.hamcrest.Matcher matcher, Object... additionalKeyMatcherPairs)
ResponseSpecificationResponseSpecification.body(String, java.util.List, org.hamcrest.Matcher, Object...) expect that you can pass arguments to the path. This
is useful in situations where you have e.g. pre-defined variables that constitutes the path:
String someSubPath = "else";
int index = 1;
expect().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:
expect().
root("filters.filterConfig[%d].filterConfigGroups.find { it.name == 'Gold' }.includes").
body(withArgs(0), hasItem("first")).
body(withArgs(1), hasItem("second")).
..
The path and arguments follows the standard formatting syntax of Java.
Note that withArgs can be statically imported from the com.jayway.restassured.RestAssured class.
content in interface ResponseSpecificationkey - The body pathmatcher - The hamcrest matcher that must response body must match.additionalKeyMatcherPairs - Optionally additional hamcrest matchers that must return true.ResponseSpecification.content(String, org.hamcrest.Matcher, Object...)public ResponseLogSpecification log()
ResponseSpecificationResponseLogSpecification that allows you to log different parts of the ResponseSpecification.
This is mainly useful for debug purposes when writing your tests. It's a shortcut for:
given().filter(ResponseLoggingFilter.responseLogger()). ..
log in interface ResponseSpecificationpublic ResponseSpecification when()
ResponseSpecification
expect().body(containsString("OK")).when().get("/something");
is that same as:
expect().body(containsString("OK")).get("/something");
when in interface ResponseSpecificationpublic ResponseSpecification response()
ResponseSpecification
expect().response().body(containsString("OK")).when().get("/something");
is that same as:
expect().body(containsString("OK")).get("/something");
response in interface ResponseSpecificationpublic RequestSpecification given()
ResponseSpecification
expect().body(containsString("OK")).given().parameters("param1", "value1").when().get("/something");
given in interface ResponseSpecificationpublic ResponseSpecification that()
ResponseSpecification
expect().that().body(containsString("OK")).when().get("/something");
is that same as:
expect().body(containsString("OK")).get("/something");
that in interface ResponseSpecificationpublic RequestSpecification request()
ResponseSpecification
expect().body(containsString("OK")).and().request().parameters("param1", "value1").when().get("/something");
request in interface ResponseSpecificationpublic Response get(String path, Object... pathParams)
RequestSenderOptionspath. Normally the path doesn't have to be fully-qualified e.g. you don't need to
specify the path as http://localhost:8080/path. In this case it's enough to use /path.get in interface RequestSenderOptions<Response>path - The path to send the request to.pathParams - The path parameters. E.g. if path is "/book/{hotelId}/{roomNumber}" you can do get("/book/{hotelName}/{roomNumber}", "Hotels R Us", 22);.public Response post(String path, Object... pathParams)
RequestSenderOptionspath. Normally the path doesn't have to be fully-qualified e.g. you don't need to
specify the path as http://localhost:8080/path. In this case it's enough to use /path.post in interface RequestSenderOptions<Response>path - The path to send the request to.pathParams - The path parameters. E.g. if path is "/book/{hotelId}/{roomNumber}" you can do post("/book/{hotelName}/{roomNumber}", "Hotels R Us", 22);.public Response put(String path, Object... pathParams)
RequestSenderOptionspath. Normally the path doesn't have to be fully-qualified e.g. you don't need to
specify the path as http://localhost:8080/path. In this case it's enough to use /path.put in interface RequestSenderOptions<Response>path - The path to send the request to.pathParams - The path parameters. E.g. if path is "/book/{hotelId}/{roomNumber}" you can do put("/book/{hotelName}/{roomNumber}", "Hotels R Us", 22);.public Response delete(String path, Object... pathParams)
RequestSenderOptionspath. Normally the path doesn't have to be fully-qualified e.g. you don't need to
specify the path as http://localhost:8080/path. In this case it's enough to use /path.delete in interface RequestSenderOptions<Response>path - The path to send the request to.pathParams - The path parameters. E.g. if path is "/book/{hotelId}/{roomNumber}" you can do delete("/book/{hotelName}/{roomNumber}", "Hotels R Us", 22);.public Response patch(String path, Object... pathParams)
RequestSenderOptionspath. Normally the path doesn't have to be fully-qualified e.g. you don't need to
specify the path as http://localhost:8080/path. In this case it's enough to use /path.patch in interface RequestSenderOptions<Response>path - The path to send the request to.pathParams - The path parameters. E.g. if path is "/book/{hotelId}/{roomNumber}" you can do head("/book/{hotelName}/{roomNumber}", "Hotels R Us", 22);.public Response options(String path, Object... pathParams)
RequestSenderOptionspath. Normally the path doesn't have to be fully-qualified e.g. you don't need to
specify the path as http://localhost:8080/path. In this case it's enough to use /path.options in interface RequestSenderOptions<Response>path - The path to send the request to.pathParams - The path parameters. E.g. if path is "/book/{hotelId}/{roomNumber}" you can do head("/book/{hotelName}/{roomNumber}", "Hotels R Us", 22);.public Response get(URI uri)
RequestSenderOptionsuri.get in interface RequestSenderOptions<Response>uri - The uri to send the request to.public Response post(URI uri)
RequestSenderOptionsuri.post in interface RequestSenderOptions<Response>uri - The uri to send the request to.public Response put(URI uri)
RequestSenderOptionsuri.put in interface RequestSenderOptions<Response>uri - The uri to send the request to.public Response delete(URI uri)
RequestSenderOptionsuri.delete in interface RequestSenderOptions<Response>uri - The uri to send the request to.public Response head(URI uri)
RequestSenderOptionsuri.head in interface RequestSenderOptions<Response>uri - The uri to send the request to.public Response patch(URI uri)
RequestSenderOptionsuri.patch in interface RequestSenderOptions<Response>uri - The uri to send the request to.public Response options(URI uri)
RequestSenderOptionsuri.options in interface RequestSenderOptions<Response>uri - The uri to send the request to.public Response get(URL url)
RequestSenderOptionsurl.get in interface RequestSenderOptions<Response>url - The url to send the request to.public Response post(URL url)
RequestSenderOptionsurl.post in interface RequestSenderOptions<Response>url - The url to send the request to.public Response put(URL url)
RequestSenderOptionsurl.put in interface RequestSenderOptions<Response>url - The url to send the request to.public Response delete(URL url)
RequestSenderOptionsurl.delete in interface RequestSenderOptions<Response>url - The url to send the request to.public Response head(URL url)
RequestSenderOptionsurl.head in interface RequestSenderOptions<Response>url - The url to send the request to.public Response patch(URL url)
RequestSenderOptionsurl.patch in interface RequestSenderOptions<Response>url - The url to send the request to.public Response options(URL url)
RequestSenderOptionsurl.options in interface RequestSenderOptions<Response>url - The url to send the request to.public Response get()
RequestSenderOptionshttp://localhost:8080).get in interface RequestSenderOptions<Response>public Response post()
RequestSenderOptionshttp://localhost:8080).post in interface RequestSenderOptions<Response>public Response put()
RequestSenderOptionshttp://localhost:8080).put in interface RequestSenderOptions<Response>public Response delete()
RequestSenderOptionshttp://localhost:8080).delete in interface RequestSenderOptions<Response>public Response head()
RequestSenderOptionshttp://localhost:8080).head in interface RequestSenderOptions<Response>public Response patch()
RequestSenderOptionshttp://localhost:8080).patch in interface RequestSenderOptions<Response>public Response options()
RequestSenderOptionshttp://localhost:8080).options in interface RequestSenderOptions<Response>public Response head(String path, Object... pathParams)
RequestSenderOptionspath. Normally the path doesn't have to be fully-qualified e.g. you don't need to
specify the path as http://localhost:8080/path. In this case it's enough to use /path.head in interface RequestSenderOptions<Response>path - The path to send the request to.pathParams - The path parameters. E.g. if path is "/book/{hotelId}/{roomNumber}" you can do head("/book/{hotelName}/{roomNumber}", "Hotels R Us", 22);.public Response get(String path, Map pathParams)
RequestSenderOptionspath. Normally the path doesn't have to be fully-qualified e.g. you don't need to
specify the path as http://localhost:8080/path. In this case it's enough to use /path.get in interface RequestSenderOptions<Response>path - The path to send the request to.pathParams - The path parameters.public Response post(String path, Map pathParams)
RequestSenderOptionspath. Normally the path doesn't have to be fully-qualified e.g. you don't need to
specify the path as http://localhost:8080/path. In this case it's enough to use /path.post in interface RequestSenderOptions<Response>path - The path to send the request to.pathParams - The path parameters.public Response put(String path, Map pathParams)
RequestSenderOptionspath. Normally the path doesn't have to be fully-qualified e.g. you don't need to
specify the path as http://localhost:8080/path. In this case it's enough to use /path.put in interface RequestSenderOptions<Response>path - The path to send the request to.pathParams - The path parameters.public Response delete(String path, Map pathParams)
RequestSenderOptionspath. Normally the path doesn't have to be fully-qualified e.g. you don't need to
specify the path as http://localhost:8080/path. In this case it's enough to use /path.delete in interface RequestSenderOptions<Response>path - The path to send the request to.pathParams - The path parameters.public Response head(String path, Map pathParams)
RequestSenderOptionspath. Normally the path doesn't have to be fully-qualified e.g. you don't need to
specify the path as http://localhost:8080/path. In this case it's enough to use /path.head in interface RequestSenderOptions<Response>path - The path to send the request to.pathParams - The path parameters.public Response patch(String path, Map pathParams)
RequestSenderOptionspath. Normally the path doesn't have to be fully-qualified e.g. you don't need to
specify the path as http://localhost:8080/path. In this case it's enough to use /path.patch in interface RequestSenderOptions<Response>path - The path to send the request to.pathParams - The path parameters.public Response options(String path, Map pathParams)
RequestSenderOptionspath. Normally the path doesn't have to be fully-qualified e.g. you don't need to
specify the path as http://localhost:8080/path. In this case it's enough to use /path.options in interface RequestSenderOptions<Response>path - The path to send the request to.pathParams - The path parameters.public ResponseSpecification parser(String contentType, Parser parser)
ResponseSpecification
expect().body("document.child", equalsTo("something"))..
Since application/vnd.uoml+xml 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:
expect().parser("application/vnd.uoml+xml", Parser.XML).when(). ..;
You can also specify by it for every response by using:
RestAssured.registerParser("application/vnd.uoml+xml", Parser.XML);
parser in interface ResponseSpecificationcontentType - The content-type to registerparser - The parser to use when verifying the response.public ResponseSpecification and()
ResponseSpecification
expect().body(containsString("OK")).and().body(containsString("something else")).when().get("/something");
is that same as:
expect().body(containsString("OK")).body(containsString("something else")).when().get("/something");
and in interface ResponseSpecificationpublic RequestSpecification with()
ResponseSpecification
expect().body(containsString("OK")).and().with().request().parameters("param1", "value1").get("/something");
with in interface ResponseSpecificationpublic ResponseSpecification then()
ResponseSpecification
expect().body(containsString("OK")).then().get("/something");
is that same as:
expect().body(containsString("OK")).get("/something");
then in interface ResponseSpecificationpublic ResponseSpecification expect()
ResponseSpecification
expect().body(containsString("OK")).and().expect().body(containsString("something else")).when().get("/something");
is that same as:
* expect().body(containsString("OK")).and().body(containsString("something else")).when().get("/something");
expect in interface ResponseSpecificationpublic ResponseSpecification rootPath(String rootPath)
ResponseSpecification
expect().
body("x.y.firstName", is(..)).
body("x.y.lastName", is(..)).
body("x.y.age", is(..)).
body("x.y.gender", is(..)).
when().
get(..);
you can use a root path and do:
expect().
rootPath("x.y").
body("firstName", is(..)).
body("lastName", is(..)).
body("age", is(..)).
body("gender", is(..)).
when().
get(..);
Note that this method is exactly the same as ResponseSpecification.root(String).rootPath in interface ResponseSpecificationrootPath - The root path to use.public ResponseSpecification root(String rootPath)
ResponseSpecification
expect().
body("x.y.firstName", is(..)).
body("x.y.lastName", is(..)).
body("x.y.age", is(..)).
body("x.y.gender", is(..)).
when().
get(..);
you can use a root and do:
expect().root("x.y").
body("firstName", is(..)).
body("lastName", is(..)).
body("age", is(..)).
body("gender", is(..)).
when().
get(..);
Note that this method is exactly the same as ResponseSpecification.rootPath(String) but slightly shorter.root in interface ResponseSpecificationrootPath - The root path to use.public ResponseSpecification noRoot()
ResponseSpecification
expect().
root("x.y").
body("firstName", is(..)).
body("lastName", is(..)).
noRoot()
body("z.something1", is(..)).
body("w.something2", is(..)).
when().
get(..);
This is the same as calling rootPath("") but more expressive.
Note that this method is exactly the same as ResponseSpecification.noRootPath() but slightly shorter.noRoot in interface ResponseSpecificationResponseSpecification.root(String)public ResponseSpecification noRootPath()
ResponseSpecification
expect().
root("x.y").
body("firstName", is(..)).
body("lastName", is(..)).
noRoot()
body("z.something1", is(..)).
body("w.something2", is(..)).
when().
get(..);
This is the same as calling rootPath("") but more expressive.
Note that this method is exactly the same as ResponseSpecification.noRoot() but slightly more expressive.noRootPath in interface ResponseSpecificationResponseSpecification.root(String)public ResponseSpecification appendRoot(String pathToAppend)
ResponseSpecification
expect().
root("x.y").
body("age", is(..)).
body("gender", is(..)).
body("name.firstName", is(..)).
body("name.lastName", is(..)).
when().
get(..);
you can use a append root and do:
expect().
root("x.y").
body("age", is(..)).
body("gender", is(..)).
appendRoot("name").
body("firstName", is(..)).
body("lastName", is(..)).
when().
get(..);
appendRoot in interface ResponseSpecificationpathToAppend - The root path to append.public ResponseSpecification appendRoot(String pathToAppend, List<Argument> arguments)
ResponseSpecification
String namePath = "name";
expect().
root("x.y").
body("age", is(..)).
body("gender", is(..)).
body(namePath + "first", is(..)).
body(namePath + "last", is(..)).
when().
get(..);
you can use a append root and do:
String namePath = "name";
expect().
root("x.y").
body("age", is(..)).
body("gender", is(..)).
appendRoot("%s", withArgs(namePath)).
body("first", is(..)).
body("last", is(..)).
when().
get(..);
appendRoot in interface ResponseSpecificationpathToAppend - The root path to use. The path and arguments follows the standard formatting syntax of Java.public ResponseSpecification detachRoot(String pathToDetach)
ResponseSpecification
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(..)).
detachRoot in interface ResponseSpecificationpathToDetach - The root path to detach.public ResponseSpecification rootPath(String rootPath, List<Argument> arguments)
ResponseSpecificationResponseSpecification.root(String, java.util.List).rootPath in interface ResponseSpecificationrootPath - The root path to use.arguments - A list of arguments. The path and arguments follows the standard formatting syntax of Java.ResponseSpecification.rootPath(String)public ResponseSpecification root(String rootPath, List<Argument> arguments)
ResponseSpecificationResponseSpecification.rootPath(String, java.util.List).root in interface ResponseSpecificationrootPath - The root path to use.arguments - The list of substitution arguments. The path and arguments follows the standard formatting syntax of Java..ResponseSpecification.rootPath(String)public boolean hasBodyAssertionsDefined()
public boolean hasAssertionsDefined()
public ResponseSpecification defaultParser(Parser parser)
ResponseSpecificationexpect().defaultParser(Parser.JSON).when(). ..;You can also specify it for every response by using:
RestAssured.defaultParser(Parser.JSON);
defaultParser in interface ResponseSpecificationparser - The parser to use when verifying the response if no other parser is found for the response content-type.public ResponseSpecification contentType(ContentType contentType)
ResponseSpecificationcontentType.
Note that this will affect the way the response is decoded.
E,g. if you can't use JSON/XML matching (see e.g. ResponseSpecification.body(String, 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 in interface ResponseSpecificationcontentType - The content type of the response.public ResponseSpecification contentType(String contentType)
ResponseSpecificationcontentType.
Note that this will affect the way the response is decoded.
E,g. if you can't use JSON/XML matching (see e.g. ResponseSpecification.body(String, 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 in interface ResponseSpecificationcontentType - The content type of the response.public ResponseSpecification contentType(org.hamcrest.Matcher<? super String> contentType)
ResponseSpecificationcontentType.contentType in interface ResponseSpecificationcontentType - The expected content type of the response.public org.hamcrest.Matcher<Integer> getStatusCode()
getStatusCode in interface FilterableResponseSpecificationnull).public org.hamcrest.Matcher<String> getStatusLine()
getStatusLine in interface FilterableResponseSpecificationnull).public boolean hasHeaderAssertions()
hasHeaderAssertions in interface FilterableResponseSpecificationtrue if any header assertions are definedpublic boolean hasCookieAssertions()
hasCookieAssertions in interface FilterableResponseSpecificationtrue if any cookie assertions are definedpublic String getResponseContentType()
getResponseContentType in interface FilterableResponseSpecificationpublic String getRootPath()
getRootPath in interface FilterableResponseSpecificationpublic void throwIllegalStateExceptionIfRootPathIsNotDefined(String description)
public LogRepository getLogRepository()
Copyright © 2010–2014. All rights reserved.