Skip to content

Root path and path arguments

To avoid duplicated paths in body expectations you can specify a root path. E.g. instead of writing:

when().
get("/something").
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:

when().
get("/something").
then().
rootPath("x.y").
body("firstName", is(..)).
body("lastName", is(..)).
body("age", is(..)).
body("gender", is(..));

You can also set a default root path using:

RestAssured.rootPath = "x.y";

In more advanced use cases it may also be useful to append additional root arguments to existing root arguments. To do this you can use the appendRootPath method, for example:

when().
get("/jsonStore").
then().
rootPath("store.%s", withArgs("book")).
body("category.size()", equalTo(4)).
appendRootPath("%s.%s", withArgs("author", "size()")).
body(withNoArgs(), equalTo(4));

It’s also possible to detach a root. For example:

when().
get("/jsonStore").
then().
rootPath("store.category").
body("size()", equalTo(4)).
detachRootPath("category").
body("size()", equalTo(1));

Since REST Assured 7.0.0, the short forms root(..), noRoot(), appendRoot(..) and detachRoot(..), deprecated since REST Assured 4.0.0, are removed, and so is setRoot(..) of JsonPath and XmlPath. Use rootPath(..), noRootPath(), appendRootPath(..), detachRootPath(..) and setRootPath(..).

Path arguments are useful in situations where you have e.g. pre-defined variables that constitutes the path. For example

String someSubPath = "else";
int index = 1;
get("/x").then().body("something.%s[%d]", withArgs(someSubPath, index), equalTo("some value")). ..

will expect that the body path “something.else[0]” is equal to “some value”.

Another usage is if you have complex root paths and don’t wish to duplicate the path for small variations:

when().
get("/x").
then().
rootPath("filters.filterConfig[%d].filterConfigGroups.find { it.name == 'GroupName' }.includes").
body(withArgs(0), hasItem("first")).
body(withArgs(1), hasItem("second")).
..

The path arguments follows the standard formatting syntax of Java.

Note that the withArgs method can be statically imported from the io.restassured.RestAssured class.

Sometimes it’s also useful to validate a body without any additional arguments when all arguments have already been specified in the root path. This is where withNoArgs come into play. For example:

when().
get("/jsonStore").
then().
rootPath("store.%s", withArgs("book")).
body("category.size()", equalTo(4)).
appendRootPath("%s.%s", withArgs("author", "size()")).
body(withNoArgs(), equalTo(4));