API testing with Java and REST Assured - getting started
Tags: java, testing, codequality, tutorial
Published: 2020-11-08 15:32:03



# Overview

[REST Assured](https://rest-assured.io/) is an open-sourced Java library that facilitates automated testing of API endpoints.  For this post I want to walk through the steps you need to get some basic REST Assured tests up and running on your local machine.

Before diving into the nuts and bolts of REST Assured, what is API automated testing?  

The test pyramid visually depicts test types and gives some general guidelines on how many of each to create.  We want lots of unit tests and a small number of end-to-end UI tests.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/o647yzh0x76avz82iruf.png)

In the middle of the pyramid is API testing.  API tests validate the application from the API level, and do not include the user interface(UI).  Because of that, API tests will typically run faster and be less brittle than UI tests.

In the past I’ve used tools such as [Postman](https://www.getpostman.com/) and [Karate](https://github.com/intuit/karate) to do automated API testing.  I've recently starting exploring REST Assured, providing me another API testing tool.



#Gherkin and API testing
Gherkin is typically used to describe application behavior from an end-user perspective, and can be used with tools like Cucumber to create automated tests.  Gherkin uses the familiar Given/When/Then syntax.  

REST Assured uses a fluent style API that leverages those same keywords to provide a common reference point when structuring tests.  This will make your tests more readable and easy to understand.



#Hello World
So what does a REST Assured test look like?  Let's start with the basics.

```java
    @Test
    public void checkEmployeeStatus() {
        // @formatter:off
        given().
        when().
                get("http://dummy.restapiexample.com/api/v1/employees").
        then().
                assertThat().
                statusCode(200);
        // @formatter:on
    }
```

This example is probably fairly self explanatory.  We're just making a GET request and verifying the the status code is 200.  

Now let's look at what you need to do to get this simple example running on your machine.

#Your OS and IDE
Ultimately we are just building Java classes that will leverage the REST Assured library.  You can run the tests on MacOS, Linux or Windows.  You'll also need the [Java JDK](https://www.oracle.com/technetwork/java/javase/downloads/index.html) installed.

For the IDE, you can use whatever you are comfortable with.  I am using the [Community Edition of IntelliJ IDEA](https://www.jetbrains.com/idea/download), but of course these concepts are going to apply to IDEs like Eclipse, or even a simple text editor.  For this walkthrough I will be referencing IntelliJ IDEA.

#Creating the Project
Open IDEA and create a new Maven project.  To get our simple example running we're going to need to do a couple things.
- Update your pom file to include the REST Assured and TestNG dependencies:
```java
<dependencies>
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>rest-assured</artifactId>
            <version>4.2.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.3.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
```
- Create the Java class in `src/test/java`:

```java
import org.testng.annotations.Test;

import static io.restassured.RestAssured.given;

public class HelloWorld {

    @Test
    public void checkEmployeeStatus() {
        // @formatter:off
        given().
        when().
                get("http://dummy.restapiexample.com/api/v1/employees").
        then().
                assertThat().
                statusCode(200);
        // @formatter:on
    }
}
```
Right-click the test method and run the test:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/l7ivzzh4qnrae8x0dni2.png)

If you get a TestNG error "TestNG by default disables loading DTD from unsecured Url", just follow the instructions in the message and use the argument `[-Dtestng.dtd.http=true]` in your run config.

Hopefully you see a successful test:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/bztmj0uheq9r2cg9h9cx.png)

#Make the Test Fail
Is this test really doing what we think it is?  Let's find out by making it fail.  Change the endpoint to something invalid, like `http://dummy.restapiexample.com/api/v1/employees-invalid`

Run the test again.  It should fail with message `Expected status code <200> but was <404>`. That's good, because that shows our test is working as we expect, and also shows we get a 404 with an invalid endpoint.  Go ahead and change back to the valid URL back and again verify it passes.

Now let's create another test with an invalid endpoint to verify we get the expected 404:
```java
@Test
    public void checkInvalidUrlReturns404() {
        // @formatter:off
        given().
        when().
                get("http://dummy.restapiexample.com/api/v1/employees-invalid").
        then().
                assertThat().
                statusCode(404);
        // @formatter:on
    }
```
Run this test and verify it's green too.

### Viewing data Returned from the Endpoint
So now we have a couple basic tests running.  Let's go back to our first test with the valid endpoint.  What data is returned from that endpoint?

Our current test is not validating any content, and there are no logs to display the returned data.  Let's change that by adding `log().body()` to the test:
```java
@Test
    public void checkEmployeeStatus() {
        // @formatter:off
        given().
        when().
                get("http://dummy.restapiexample.com/api/v1/employees").
        then().
                assertThat().
                statusCode(200).
                log().body();
        // @formatter:on
    }
```
Run the test.  You should see the JSON response in your console log:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/fy76511yiqaw1m9agsyc.png)

That's cool, but in the real world you may only want to log on failures.  That can be accomplished with `log().ifValidationFails()`.  Go ahead and update the test to only log if the test fails.  The `ifValidationFails` change needs to be applied prior to validating the status code, like this:

```java
 @Test
    public void checkEmployeeStatus() {
        // @formatter:off
        given().
        when().
                get("http://dummy.restapiexample.com/api/v1/employees").
        then().
                assertThat().
                log().ifValidationFails().
                statusCode(200);
        // @formatter:on
    }
```

Of course you should verify it's working as expected by making it fail and verifying you get the log.

#Code Formatting
You may have noticed that the REST Assured statements are wrapped in `@formatter:off` and `@formatter:on` tags.  I do this because I like to do indentation underneath the gherkin lines (given/when/then/and), but the IDEA `code reformat` feature doesn't follow that standard. I turn the formatter off because IDEA `code reformat` will wipe out my indentation formatting if I don't.

For this to work, I also needed to "enable formatter markers in comments" in code style preferences of IDEA. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/a72iy2rfb3bx8yn35t7q.png)

#Wrap-up
So hopefully that gives you a taste of the power of REST Assured.  In my [next post](https://dev.to/leading-edje/api-testing-with-java-and-the-beatles-4624) I am going to cover some more advanced REST Assured topics such as validating payload, validating JSON schema, and POSTs and DELETEs.  

You can find the complete code for this series of blog posts in [my Github project](https://github.com/dwwhalen/HelloRESTAssured).

Happy testing!

<a href="https://dev.to/leading-edje">
  ![Smart EDJE Image](https://dev-to-uploads.s3.amazonaws.com/i/5uo60qforg9yqdpgzncq.png)
<a/>