Skip to content

Path engines and static imports

Since REST Assured 7.0.0, REST Assured doesn’t include a path engine, and so no Groovy either. If your tests use paths, such as body("lotto.lottoId", equalTo(5)), path(..) or extract().path(..), add GPath, the path syntax used throughout this page, or, for JSON, JSONPath with Jayway. It comes in two modules, json-path-gpath for JSON and xml-path-gpath for XML and HTML:

You use You need
A path on a JSON body: body("a.b", ..), path(..), extract().path(..), rootPath(..) json-path-gpath
A path on an XML or HTML body xml-path-gpath
jsonPath() or JsonPath json-path-gpath
xmlPath(), htmlPath() or XmlPath xml-path-gpath
Anything else, such as statusCode(..), headers, cookies, body(containsString(..)), hasXPath(..), JSON Schema validation or deserialization with as(..) Nothing

The same goes for RestAssuredMockMvc, RestAssuredWebTestClient and the Kotlin and Scala extensions.

<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>json-path-gpath</artifactId>
<version>7.0.0-RC1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>xml-path-gpath</artifactId>
<version>7.0.0-RC1</version>
<scope>test</scope>
</dependency>

Without a path engine, the first path fails with a message that names the dependency to add. rest-assured-all already contains both modules. Before 7.0.0, rest-assured includes them under their old names, json-path and xml-path, and json-path-gpath and xml-path-gpath resolve to those. See the release notes for how to upgrade, and Which module do I need for the other modules.

Since REST Assured 7.0.0, you can also write JSON paths in JSONPath instead, evaluated by Jayway JsonPath, with json-path-jayway. It’s for JSON only and doesn’t need Groovy:

<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>json-path-jayway</artifactId>
<version>7.0.0-RC1</version>
<scope>test</scope>
</dependency>
get("/lotto").then().body("$.lotto.winners[*].winnerId", hasItems(23, 54));
get("/store").then().body("$.store.book[?(@.price < 10)].title", hasItems("Sayings of the Century", "Moby Dick"));

The examples on the rest of this page are GPath. In JSONPath, a list needs [*]: GPath’s lotto.winners.winnerId is lotto.winners[*].winnerId, and Jayway fails on the GPath form with a message that says so. Use filters such as [?(@.price < 10)] instead of GPath’s closures. jsonPath() is GPath, so with Jayway use JaywayPath.from(response.asString()), which returns Jayway’s DocumentContext.

With only json-path-jayway on the classpath, REST Assured uses it. With both json-path-gpath and json-path-jayway, it uses GPath and logs once that it does. Pick Jayway for all tests, a request or a specification with JsonConfig.pathEngine(..):

RestAssured.config = RestAssured.config().jsonConfig(jsonConfig().pathEngine(JsonPathEngine.JAYWAY));
given().config(config().jsonConfig(jsonConfig().pathEngine(JsonPathEngine.JAYWAY))). ..
new ResponseSpecBuilder().setConfig(config().jsonConfig(jsonConfig().pathEngine(JsonPathEngine.JAYWAY))). ..

Jayway logs through SLF4J. See the release notes for the supported Jayway versions, the differences from GPath and OSGi.

In order to use REST assured effectively it’s recommended to statically import methods from the following classes:

io.restassured.RestAssured.*
io.restassured.matcher.RestAssuredMatchers.*
org.hamcrest.Matchers.*

If you want to use Json Schema validation you should also statically import these methods:

io.restassured.module.jsv.JsonSchemaValidator.*

See JSON Schema validation for more.

If you’re using Spring MVC you can use the spring-mock-mvc module to unit test your Spring Controllers using the Rest Assured DSL. To do this statically import the methods from RestAssuredMockMvc instead of importing the methods from io.restassured.RestAssured:

io.restassured.module.mockmvc.RestAssuredMockMvc.*