When unit testing, executing a test class requires starting the springboot project, loading the context data, and reloading the context each time a test is executed. This results in 3-5 minutes of time spent on each unit test. How to solve this problem?
The thing is that you don't need to load the full context for unit tests.
@SpringBootTest
annotation loads the entire Spring context. Reserve it for integration tests or when you need the full context (it depends on what kind of tests you are interested in).
For unit tests (say testing service classes), consider using Mockito framework. The @MockBean
annotation provided by Spring Boot allows you to add mock objects to the Spring application context. This can significantly reduce the startup time as it avoids loading the actual beans and their dependencies. Unit tests are supposed to be fast and isolated, these tests don't require the full application context.