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…