Testing Spring Boot with REST Assured

Spring Boot and REST Assured is a great combination to build and test REST Web services. In this tutorial we will learn how to create a simple REST application and test it with REST Assured Framework.

REST Assured is a Java library that allows you to use a domain-specific language (DSL) for writing powerful, easy to maintain tests for RESTful APIs. In this tutorial I’ll show you how to set up and configure REST Assured with a Spring Boot Application.

Start by creating a new project which includes the web dependencies:

  spring init -dweb demo-test 

Now import the Maven project in your favourite IDE and add a model class named Customer that will be returned by a GET Request:

package com.example.demorest;

public class Customer {
  private int id;
  private String name;

  public Customer(int id, String name) {
    super();
    this.id = id;
    this.name = name;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public int getId() {
    return id;
  }

  public void setId(int id) {
    this.id = id;
  }
}

All you need to build a Rest Service in Spring Boot is a RestController:

package com.example.testrest;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class CustomerController {
  @Autowired CustomerRepository repository;

  @RequestMapping("/list")
  public List<Customer> findAll() {
    return repository.getData();
  }

  @RequestMapping("/one/{id}")
  public Customer findOne(@PathVariable int id) {
    return repository.getData().get(id);
  }
}

As you can see, the CustomerController Endpoint has two methods:

  • findAll: which returns all the Customer objects
  • findOne: which returns a single Customer object

Both methods rely on the CustomerRepository Class which has a very simple implementation to produce a List of Customer objects:

package com.example.testrest;

import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

@Component
public class CustomerRepository {
  List<Customer> customerList = new ArrayList<Customer>();

  @PostConstruct
  public void init() {
    customerList.add(new Customer(1, "frank"));
    customerList.add(new Customer(2, "john"));
  }

  public List<Customer> getData() {
    return customerList;
  }
}

And finally let’s code the Test class which uses REST Assured to verify both methods:

package com.example.testrest;

import org.hamcrest.Matchers;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import static io.restassured.RestAssured.get;
import static org.hamcrest.CoreMatchers.is;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class DemoApplicationTests {
  @Test
  public void testCustomerList() {
    get("http://localhost:8080/list").then().assertThat().statusCode(200).body("size()", is(2));
    get("http://localhost:8080/one/0")
        .then()
        .assertThat()
        .statusCode(200)
        .body("name", Matchers.equalTo("frank"));
    get("http://localhost:8080/one/1")
        .then()
        .assertThat()
        .statusCode(200)
        .body("name", Matchers.equalTo("john"));
  }
}

In order to compile the application, you need the RestAssured dependency available in your pom.xml:

<?xml version="1.0" encoding="UTF-8"?><project>
   <dependency>
       	
      <groupId>io.rest-assured</groupId>
       	
      <artifactId>rest-assured</artifactId>
       	
      <version>3.0.7</version>
       	
      <scope>test</scope>
       
   </dependency>
</project>

Now compile and package the application with:

$ mvn clean install 

Now you can run the application in any Spring Boot available way, f.e.

mvn spring-boot:run 

Then, run the Test class as well, which should pass all Tests contained:

spring boot restassured tutorial

You can find the source code for the Spring Boot REST Assured tutorial here: https://github.com/fmarchioni/masterspringboot/tree/master/test/test-rest