Logo

dev-resources.site

for different kinds of informations.

Spring Boot Integration Testing with MockMvc

Published at
10/22/2022
Categories
mockmvc
spring
java
junit
Author
roshanadh
Categories
4 categories in total
mockmvc
open
spring
open
java
open
junit
open
Author
9 person written this
roshanadh
open
Spring Boot Integration Testing with MockMvc

In this piece, we will build a REST-ful API using Spring Boot and Spring MVC, then perform integration testing by making requests to our endpoints and verifying the responses. For this project, we will need JUnit, MockMvc, H2, and a few other dependencies. Let's get started!

Dependencies

First, go to the Spring Initializr and generate a Spring Web application using the following dependencies:
  1. Spring Web
  2. Spring Data JPA
  3. H2 Database
  4. Lombok

Configuring Spring Boot dependencies with Spring Initializr


We will create a REST-ful API, expose some endpoints, and test those endpoints using MockMvc.For persistence support, we will use the H2 in-memory database, and for interacting with the database, we will use the repositories provided by Spring Data JPA. I will be using Lombok for reducing the amount of boilerplate code I would be writing otherwise.

Controllers

For the purposes of brevity, we will be exposing just the following endpoints and HTTP actions:
  1. GET /batteries - Returns a list of all Battery resources in database
  2. GET /batteries/:id - Returns the Battery resource identified by id path variable
  3. POST /batteries - Persists a Battery resource in database and returns the persisted resource

Resource

As you can see from the Controllers section, we will be working on a Battery resource for our API. The Battery resource will have the following fields:

Also, using the JPA annotations @Entity, @Table, @Column, etc., we map the POJO to our database entity of name batteries.

Repository

One of the most useful features of the Spring Data JPA project is the Repository pattern that comes with its implementations out-of-the-box. This way, we can quickly bootstrap a CRUD functionality without having to writing queries by ourselves.
Let us define the repository for our Battery entity by extending the JpaRepository interface.

Having defined this repository, we can now access a bunch of predefined methods that implement CRUD functionality (e.g.,  save, saveAll, deleteById, delete, etc.)

Bootstrap the database

To test our application, we will need some preloaded records. Let us create a Bootstrap configuration class that loads records into our database every time the application starts.

When a class is annotated with @Configuration, it means that the Spring IOC container can expect some bean definition inside the class. And so we give a bean definition using the @Bean annotation on the initDb method that returns an object of type CommandLineRunner.


CommandLineRunner is a functional interface with a method named run. When the Spring context is loaded, Spring Boot invokes the run method of all beans of type CommandLineRunner.

Application properties

For our application to work, we need to pass some application properties, including those for the database URL, username, password, and driver class name. Additionally, you can pass properties to enable the H2 console through which you can access the database using a GUI.

Testing our API

The spring-boot-starter-test starter module included in our pom.xml file includes JUnit, AssertJ, Hamcrest, etc. All of these libraries help us write effective test cases for our projects.
For this particular project, start with the main test class:
The @SpringBootTest annotation is used on tests classes for Spring Boot, and it loads the complete Spring application context.
Using @AutoConfigureMockMvc, we can enable auto-configuration of the Spring MVC test framework. MockMvc does request handling but uses mock request and response objects. No actual server is started.

Testing GET /batteries/:id

As you can see, we have imported some static methods from MockMvcRequestBuilders, MockMvcResultHandlers, and MockMvcResultMatchers. These static methods are then used for making HTTP requests (get, post, etc.), reading JSON responses (jsonPath), and printing the responses (print).
With the above test case, we verify that the response for GET /batteries/1 HTTP action contains the same id, name, postcode, and capacity values as in our database (the one we preloaded).

Testing GET /batteries

Similar to the previous test, we make a GET request to our endpoint and then print the response. Then we continue our tests through andExpect. First, we verify that the response status is 200 OK. Then, we verify that the response is a list of JSON objects. Finally, we verify that the size of list is equal to the size of records that exist in the database.

Testing POST /batteries

To send a request body along with our POST request, we use the autowired ObjectMapper instance to map the Battery object onto a String. The string object is then passed as the content of our POST request, and we specify the type of request body as JSON using contentType method.
Then, we use andExpect to verify our expectations with the JSON response. The response status should should be 201 CREATED. And since our API returns the very object that was persisted, the name, postcode, and capacity parameters should match.

Conclusion

We have successfully created and tested our REST-ful API using MockMvc and several other utility testing libraries. The full project code has been pushed to GitHub, feel free to check it out.
junit Article's
30 articles in total
Favicon
verify() method in Mockito example
Favicon
Uses of @spy annotation in junit testing
Favicon
How To Install TestNG in Eclipse: Step By Step Guide
Favicon
How to simulate real BeforeAll and AfterAll in JUnit 5
Favicon
Spring Boot Testing Best Practices
Favicon
Why there are so many assertAll methods in Junit class AssertAll? What is the use of each.
Favicon
Testando das trincheiras: Como criar mocks e stubs dinâmico com mockito em java
Favicon
Mastering Selenium Testing: JUnit Asserts With Examples
Favicon
JUnit Tutorial: An Inclusive Guide [With Enhanced Features]
Favicon
Junit Badge For Git Project
Favicon
Testes Unitários com JUnit no Java
Favicon
JUnit 5 – When to use CSV Providers
Favicon
JUnit Tutorial: An Inclusive Guide [With Enhanced Features]
Favicon
Less Code, More Tests: Exploring Parameterized Tests in JUnit
Favicon
How to find bugs before sending it to production using boundary testing technique
Favicon
Test utilities, or set-up methods considered harmful
Favicon
Dominando o Poder dos Testes Unitários em Java com JUnit: Construa Código Sólido e Confiável! 🚀🧪🔨
Favicon
How to resolve the configuration problem with 2 application.properties in Springboot?
Favicon
TestNG vs JUnit: An Unbiased Comparison Between Both Testing Frameworks
Favicon
Quick introduction to EasyRandom
Favicon
JUnit Tests in Java: A Guide to Writing Effective Unit Tests
Favicon
JUnit's @CsvSource.quoteCharacter
Favicon
Creating a REST API using Spring Boot + Tests + Documentation [part 04]
Favicon
How to use Junit and Mockito for unit testing in Java
Favicon
Unit Testing JSON Functions in Android
Favicon
Adding Unit Testing to OpenSSG
Favicon
Running JMH benchmark from Eclipse
Favicon
Setting up Junit 5 Parallel Test Execution With Maven
Favicon
Integration Tests with Micronaut and Kotlin
Favicon
Spring Boot Integration Testing with MockMvc

Featured ones: