Transcripted Summary
In this chapter, I would like to give you a quick introduction to REST Assured, as well as to the API that we'll be working with in the remainder of this course.

So, what is REST Assured? REST Assured is a Java DSL, or Domain-Specific Language, that allows you to write powerful, readable, and maintainable tests in Java for your RESTful APIs. Your REST Assured tests run on top of existing unit testing frameworks like JUnit or TestNG.

What I like most about using REST Assured is that it abstracts away a lot of boilerplate code, making your tests powerful yet really easy to read and maintain. REST Assured is an open source Java project. It's developed and maintained by Johan Haleby. You can find all there is to know about REST Assured on the REST Assured main page, which is rest-assured.io.

If you want to start with working with REST Assured in your Java testing projects, it's really easy to do so. You can simply add REST Assured as a dependency in your Maven project:

<dependency>
      <groupId>io.rest-assured</groupId>
      <artifactId>rest-assured</artifactId>
      <version>RELEASE</version>
</dependency>

Or if you prefer to use Gradle instead, then that's supported as well:

testCompile 'io.rest-assured:rest-assured:3.3.0'

Now before we dive deeper into REST Assured as a test tool, I'd like to quickly introduce you to our API under test. The API that we're going to be using in the remainder of this course is Zippopotam.us, and it's a simple RESTful API that supplies location data based on the country code and the zip code that you supply in your API request.

The API has a nice website, as you can see. And if you click on countries, you can see which countries are supported by the API. So, for which countries and which zip code ranges the API will supply you with a response with location data related to that specific country code and zip code. As you can see, there are more than 60 countries features in this API, so that's more than enough test data to work with in this course.

If we want to use this API in our REST Assured tests, we can send a GET call at to this locator which contains two path parameters, namely US as a country code and 90210 as the zip code for which we want to retrieve the location data.

_GET http://api.zippopotam.us/us/90210

If you perform this request using REST Assured, the response will look something like this:

{
  post code: "90210",
  country: "United States",
  country abbreviation: "US",
  places: [
    {
      place name: "Beverly Hills",
      longitude: "-118.4065",
      state: "California",
      state abbreviation: "CA",
      latitude: "34.0901"
    }
  ]
}

And, as you can see in the response, you get a list of places associated with the country code and the zip code that you specified, and in this case, the API returns a single place, namely Beverly Hills in the state of California.

The API also returns some response headers, including the status code which indicates that everything went fine, and the Content-Type, which tells us, or rather, the consumer of the API, that the content type of the response is application/json which helps the API consumer decode the response body.

Let's take a quick look at a simple REST Assured test that represents this API call.

What I have here is a very basic REST Assured test that performs the GET request that we saw earlier and checks whether the first place name in the body in the list of places is equal to Beverly Hills.

import org.junit.Test;

import static io.restassured.RestAssured.*;
import static org.hamcrest.Matchers.*;

public class Chapter1Test {

    @Test
    public void requestUsZipCode90210_checkPlaceNameInResponseBody_expectBeverlyHills() {

        given().
        when().
            get("http://zippopotam.us/us/90210").
        then().
            assertThat().
            body("places[0].'place name'", equalTo("Beverly Hills"));
    }
}

As you can see, this REST Assured code looks really clean and even though I've inserted some line breaks for readability, it's actually just a single line of code that performs the GET request and does the assertions on the response.

Because REST Assured runs on top of JUnit or TestNG, you don't really need anything else to be able to write and run REST Assured tests in your Java project. And if you run this test, obviously it passes because the only place that's associated with the US zip code of 90210 is, of course, Beverly Hills.

In order to demonstrate to you that the test really does what it says on the tin, let's quickly change the expected value of the place name in the list of places in the API response to "New York", and then see what happens.

And now, as expected, this test fails and the underlying unit testing framework, in this case JUnit, tells us that, "Hey, the expected value is New York in the list of place names associated with the zip code 90210, but the actual value that was returned by the API is Beverly Hills."

So, let's quickly change this back to Beverly Hills. And as you can see, our test will now pass again.

In the next chapter, we'll be looking in some more detail about some of the basic features that REST Assured provides us and that can help you to write powerful yet maintainable tests in Java.

All of the code examples that you've seen in this chapter and in the chapters to come can be found on my GitHub page, so you can easily download or clone those and try them out for yourself.



Resources



© 2024 Applitools. All rights reserved. Terms and Conditions Privacy Policy GDPR