What's new in REST Assured 1.8?

REST Assured is a framework for easy testing of REST or HTTP services in Java and has recently reached version 1.8. The internals of REST Assured have undergone a major refactoring to allow JsonPath and XmlPath to be extracted to their own projects. This allows you to use these projects without depending on REST Assured itself. What does this mean and how can you benefit? Read on!

Parsing documents with Groovy GPath

Groovy has a very simple and elegant way to parse XML and JSON documents. Consider the following JSON document:

Groovy has something called a JsonSlurper which you can use to extract documents from this document:

Now consider the following XML document:

For XML Groovy provides the XmlSlurper that let’s you do e.g:

Parsing JSON documents with REST Assured JsonPath

JsonPath is essentially a wrapper around Groovys JsonSlurper that adds some additional features and makes it a little bit simpler to work with from Java. It’s a standard Maven dependency and you don’t need to configure and Groovy compilers etc, you don’t even have to know that Groovy is involved. Let’s have a look at how we can extract the very same values from the “lotto” JSON document but this time using REST Assured’s JsonPath:

You can see that we’ve duplicated “lotto” in all our paths. To avoid this we can set a root path:

If you’re only interested in extracting a single value you can do like this:

It’s also possible to do some more complex stuff, such as summing the total winning numbers:

or finding all numbers greater than 10 of the winner with id 23:

Object Mapping

One of the nice things with JsonPath is that it allows you to do object mapping using either Gson, Jackson or Jackson 2 and it’s also possible to roll your own. For example let’s say that we want to extract the first winner to a Java bean defined like this:

We can extract the first winner as a Java object:

Configuration

You can easily configure JsonPath to for example provider your own object mapper, configure existing object mappers, change charset etc etc. You can do this by supplying a configuration to JsonPath:

This will instruct JsonPath to assume content type ISO-8859-1 when parsing the document at /tmp/document.json.

Summary JsonPath

Since JsonPath now can be used stand-alone without any need to depend on REST Assured itself it can be seen as an alternative to Json-Path.

Parsing XML documents with REST Assured XmlPath

Let’s revisit the XML example:

Using XmlPath in REST Assured we can extract some values like this:

Just as with JsonPath it’s also possible to simply extract only one value:

You can also do some more complex stuff such as getting the year of the first record as an int:

What we do here is to first converting all year attributes to a list on which we invoke the toInteger() method on all elements in the list (this is needed since the year attribute in the XML documents is represented as a String). Then we invoke the min() which will return the smallest number in the list which happens to be 1931.

Object Mapping

With XmlPath you easily map an XML document or parts of an XML document to a Java Object using JAXB (you can also roll your own). Consider the following XML:

And let’s say you have a Java bean like this:

And let’s say you want to map the first greeting represented in the XML document to an instance of Greeting:

That’s it!

Configuration

You can easily configure XmlPath to for example provider your own object mapper, configure existing object mappers, change charset etc. You do this by supplying a configuration to XmlPath:

Summary

As you’ve hopefully seen parsing JSON and XML using JsonPath and XmlPath is very simple and powerful. Refer to the REST Assured web page for more info.

This Post Has 13 Comments

  1. Hi,
    I am trying to use the REST Assured framework for testing our REST API. I could not find much information on publishing the test reports and integrating with CI.
    Can you please suggest.

    Thanks for the great work.
    REST Assured is amazing.
    –pradeep

    1. Hey Pradeep,

      Not sure which CI solution you’re using.
      I’m using TestNG to organize my REST-assured cases and Maven Surefire plugin to generate reports. They work perfectly on Jenkins.

  2. Hi Max,

    Can you please provide sample code to test rest service.

    Thanks
    Umang

    1. Check out the project web site, there are links to example and tutorials from the usage page.

  3. Hi,
    I tried with response.getBody().jsonPath().getXXX methods. It would be good to have an additional method that will return a part of the response body as JSONDocument (String) itself based on the the path.
    The sub/new response can be used again for additional JsonPath processing. If this is already available, please share how this can be achieved.

    String str = response.getBody().jsonPath().getString(“some.path”);
    with(str).get(“more.sub.path”); // Getting Exception as Failed to parse the JSON document

    -Thanks
    Anver

    1. Good idea. This is not available for JsonPath but it works for XmlPath (you can get a Node object in XmlPath and by using the Node object you can continue querying the document). Please add it as an improvement suggestion on the issue tracker at the project web page.

  4. Is it possible to retrieve elements using Json-Path using
    wildcards ? for example, use $..book to retrieve book element no matter where it is in the document. I tried with json-path bundled with rest-assured, it did not work. If I switch to Json-Path 0.9.1 (http://code.google.com/p/json-path/), syntax like $..book works.

    Thanks,
    June

    1. The JsonPath project shipped with REST Assured is NOT the same JsonPath project that’s available at http://code.google.com/p/json-path/. REST Assured’s JsonPath uses Groovy GPath expressions whereas json-path uses another syntax.

  5. I am new to rest-assured and have the following code,

    String json = RestAssured.get(“/posts”).asString();
    JsonPath jsonPath = new JsonPath(json).using(new JsonPathConfig(“ISO-8859-1”));
    User user = JsonPath.from(json).getObject(“[0]”,User.class);

    When i run the code, i get the following error,
    java.lang.IllegalStateException: Cannot deserialize object because no JSON deserializer found in classpath. Please put either Jackson or Gson in the classpath.

    But, jackson-core is already in the classpath. Why am i getting this error

    1. Please use the mailing list for asking questions

  6. HI,

    Is this possible to generate API Performance test reports with Rest Assured ?

    Thanks
    Partha

    1. REST Assured is not intended for performance testing. Have a look at JMeter, Gatling Tool or something similar.

  7. Also how do I use a post parameter for nested objects in jSon: example:

    {

    “Name”: “abc”,

    “Description”: “test “,

    “Criteria”: {

    “Status”: “1,2,5,6,9,10,11”,

    “isPublic”: false

    }

    }

Leave a Reply

Close Menu