Search This Blog

Spring Boot 2 CRUD REST APIs Integration Testing

In this article, we will learn how to write great integration tests for our Spring Boot Rest APIs. We will develop Spring Boot CRUD REST APIs using Spring Boot 2, JPA, Hibernate 5,MySQL and we will write integration tests for these CRUD REST APIs.

Spring Boot provides the @SpringBootTest annotation, which uses SpringApplication behind the scenes to load ApplicationContext so that all the Spring Boot features will be available. For @SpringBootTest, we can pass Spring configuration classes, Spring bean definition XML files, and more, but in Spring Boot applications, we’ll typically use the entry point class.

The TestRestTemplate bean will be registered automatically only when @SpringBootTestis started with an embedded servlet container.

Important: A key part of integration testing is testing all the layers in the application.

Table of Contents

What we’ll build

Tools and Technologies Used

Creating and Importing a Project

Packaging Structure

The pom.xml File

Create JPA Entity - Employee.java

Create Spring Data Repository - EmployeeRepository.java

Create Spring Rest Controller - EmployeeController.java

Exception(Error) Handling for RESTful Services

Running Application

Integration Testing REST APIs

1. What we’ll build

We will build a CRUD REST APIs using Spring Boot 2, JPA and MySQL as a database. Following are five REST APIs (Controller handler methods) are created for Employee resource and we will write Integration tests for all these REST APIs.

2. Tools and Technologies Used

Spring Boot - 2.0.4.RELEASE

JDK - 1.8 or later

Spring Framework - 5.0.8 RELEASE

Hibernate - 5.2.17.Final

JPA

Maven - 3.2+

IDE - Eclipse or Spring Tool Suite (STS)

3. Creating and Importing a Project

There are many ways to create a Spring Boot application. The simplest way is to use Spring Initializr at http://start.spring.io/, which is an online Spring Boot application generator.

Look at the above diagram, we have specified following details:

Generate: Maven Project

Java Version: 1.8 (Default)

Spring Boot:2.0.4

Group: net.guides.springboot2

Artifact: springboot2-jpa-crud-example

Name: springboot2-jpa-crud-example

Description: Rest API for a Simple Employee Management Application

Package Name : net.guides.springboot2.springboot2jpacrudexample

Packaging: jar (This is the default value)

Dependencies: Web, JPA, MySQL, DevTools

Once, all the details are entered, click on Generate Project button will generate a spring boot project and downloads it. Next, Unzip the downloaded zip file and import it into your favorite IDE.

That's a cool error response. It contains all the details that are typically needed.

What happens when we throw an Exception?

Let’s see what Spring Boot does when an exception is thrown from a Resource. we can specify the Response Status for a specific exception along with the definition of the Exception with ‘@ResponseStatus’ annotation.

To use ErrorDetails to return the error response, let’s create a GlobalExceptionHandler class annotated with @ControllerAdvice annotation. This class handles exception specific and global exception in common place.

Key points about integration tests

While running the integration tests that start the embedded servlet containers, it is better to use WebEnvironment.RANDOM_PORT so that it won’t conflict with other running applications, especially in Continuous Integration (CI) environments where multiple builds run in parallel.

You can specify which configuration classes to use to build ApplicationContext by using the classes attribute of the @SpringBootTest annotation.

The TestRestTemplate bean will be registered automatically only when @SpringBootTest is started with an embedded servlet container.

As you need to test the REST endpoint, you start the embedded servlet container by specifying the webEnvironment attribute of @SpringBootTest.

The default webEnvironment value is WebEnvironment.MOCK, which doesn’t start an embedded servlet container.

You can use various webEnvironment values based on how you want to run the tests.

MOCK (default) — Loads a WebApplicationContext and provides a mock servlet environment. It will not start an embedded servlet container. If servlet APIs are not on your classpath, this mode will fall back to creating a regular non-web ApplicationContext.

RANDOM_PORT— Loads a ServletWebServerApplicationContext and starts an embedded servlet container listening on a random available port.

DEFINED_PORT—Loads a ServletWebServerApplicationContext and starts an embedded servlet container listening on a defined port (server.port).

NONE—Loads an ApplicationContext using SpringApplication but does not provide a servlet environment.