Unit Testing Using Spring Boot
Understand how to create unit tests in Spring Boot using the @SpringBootTest annotation, Mockito, and JUnit. Learn why @SpringBootTest loads the entire application context and why it suits integration rather than unit tests. Discover how to autowire beans and write test methods to validate your application logic effectively.
We'll cover the following...
When we create an application using Spring Boot, we get the springboot-starter-test dependency that comes with Mockito and AssertJ as testing libraries. We also automatically get a test file with the @SpringBootTest annotation.
Unit testing should not be done using Spring Boot. The main purpose of unit testing is to test a method or class. However, @SpringBootTest loads the entire context, which makes the test lengthy and defeats the purpose of unit testing. This feature of Spring Boot should be used in integration testing where we test across multiple layers. ...