Unit and Integration Testing Spring Boot and JUnit 5
This tutorial is intended for intermediate backend developers developing microservices using Spring Boot and JUnit 5 (Jupiter).
There are four major learning points: Spring Boot, Junit 5, Unit Testing and Integrated Testing. Test Driven Development (TDD) approach enables developers to write production grade tests during software development and the basic is Unit Testing, to verify that code written gives the functionality and output expected.
For anyone familiar with Spring, see picture above. Spring boot has a hierarchical project setup where the bootstrap class sits at the base of other classes which can be separated into controllers, models, exceptions, repository, service packages and others as needed. The various starter project generator generates folders for tests.
I won’t go into how to code of all I have mentioned. The focus is on Spring Boot, JUnit 5 unit and integrated testing.

You can read the conceptual difference between unit testing and integrated testing here. The difference between unit testing and integrated testing in Spring Boot are @WebMvcTest and @SpringBootTest annotations. For concept definitions taken from Stackoverflow:
@SpringBootTest annotation tells Spring Boot to go and look for a main configuration class (one with @SpringBootApplication for instance), and use that to start a Spring application context. SpringBootTest loads complete application and injects all the beans which can be slow.
@WebMvcTest — for testing the controller layer and you need to provide remaining dependencies required using Mock Objects.
Few more annotations below for your reference.
Testing slices of the application Sometimes you would like to test a simple “slice” of the application instead of auto-configuring the whole application. Spring Boot 1.4 introduces 4 new test annotations:
@JsonTest - for testing the JSON marshalling and unmarshalling
@DataJpaTest - for testing the repository layer
@RestClientTests - for testing REST clients
So basically we are loading the entire application using @SpringBootTest. Now that is our integration testing. For @WebMvcTest and other slice annotation, we are only loading our application partially to test different units of our application.
To conclude, I will provide below two github gists of examples. Note that the Spring Boot application runs on MySQL database. It can be in memory database. You can generate the sql from the model class. The complete project is on github. Notice the @ExtendWith(SpringExtension.class) which is the JUnit5 annotation for running test.
Unit Test.
https://gist.github.com/tksilicon/80ce56c83e9203c52cf0dade3933a82a#file-unitspringbootapplicationtests-java
Integration test.
In addition to @ExtendWith(SpringExtension.class) annotation we have @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) which loads the whole application and @TestInstance(Lifecycle.PER_CLASS). Read about TestInstance here.
https://gist.github.com/tksilicon/654e0308b8392cd9e17e2deb6a5e37cc#file-integratedsprintbootapplicationtests-java
The end!