In Short
@SpringBootTestloads full application context, exactly like how you start a Spring container when you run your Spring Boot application.
@WebMvcTestloads only the web layer, which includes security, filter, interceptors, etc for handling request/response. Typically you would write tests for methods under@Controlleror@RestController.
@DataJpaTestloads only configuration for JPA. It uses an embedded in-memory h2 if not specified otherwise.Service layer tests should ideally not have any annotations (except for ones that aid in mocking) because this is where your business logic (independent of any configurations) sits.
Regarding best practice, it's really just separation of concerns. I rarely ever used
@SpringBootTestunless it's meant for some ad-hoc integration test on my local. Annotations like@WebMvcTestkeep your tests more 'modularized' and slightly faster.
No comments:
Post a Comment