RestAssured: example of using GPath for json response

As it is described on Github page, RestAssured brings the simplicity of using Groovy language into the Java domain for testing and validating REST services. Let's take a look at some very short example of RestAssured usage. For testing we will be using resource of Wortschatz application http://wortschatz-olyv.rhcloud.com/new_lesson which returns JSON array of objects.

First of all we start with maven dependency:

<dependency>
      <groupId>io.rest-assured</groupId>
      <artifactId>rest-assured</artifactId>
      <version>3.0.3</version>
      <scope>test</scope>
</dependency>

Now we need to do some preparation for testing. Let's add a couple of fields specific for http://wortschatz-olyv.rhcloud.com, those are Accept-Language header and RestAssured specific objects ResponseSpecification and RequestSpecification. Specification objects allow to describe expected response and request accordingly (for more info about specification refer to documentation)

 private static final String ACCEPT_LANGUAGE_HEADER = "Accept-Language";
 private static final String ACCEPT_LANGUAGE_HEADER_VALUE = "en-US,en;";
 private static ResponseSpecification responseSpec;
 private static RequestSpecification requestSpec;

In setUp() method initialize our expected response and request:

 @BeforeClass
 public static void setUp() {
        RestAssured.baseURI = "http://wortschatz-olyv.rhcloud.com";

        responseSpec = new ResponseSpecBuilder()
                .expectStatusCode(200)
                .expectContentType(ContentType.JSON)
                .build();

        requestSpec = given()
                .contentType(ContentType.JSON)
                .header(ACCEPT_LANGUAGE_HEADER, ACCEPT_LANGUAGE_HEADER_VALUE);
 }

And now we are ready to write actual test method. We are going just to send a request and verify size of returned json array, for counting specific objects in array GPath is being used.

 @Test
 public void shouldReturnListOfWordsWithSpecificSizeTest() {
        Response response = requestSpec
                .when()
                .get("/new_lesson")
                .then()
                .spec(responseSpec).extract().response();

        String responseBodyString = response.getBody().asString();

        assertThat(from(responseBodyString).getList("$").size()).isEqualTo(9);
        assertThat(from(responseBodyString).getList("findAll { it.type == 'verb' }").size()).isEqualTo(3);
        assertThat(from(responseBodyString).getList("findAll { it.type == 'noun' }").size()).isEqualTo(3);
        assertThat(from(responseBodyString).getList("findAll { it.type == 'adjective' }").size()).isEqualTo(3);
 }

Note: in order to use assertThat() fluent assertions, you need to add correspondent dependency to your pom file:

<dependency>
      <groupId>org.assertj</groupId>
      <artifactId>assertj-core</artifactId>
      <version>3.8.0</version>
      <scope>test</scope>
</dependency>

For more information refer to http://joel-costigliola.github.io/assertj.

That was really short example of using RestAssured library. Hopefully you found it helpful.



Comments