Unit tests (fastest, most of the suite):
- Plain JUnit 5 and Mockito, no Spring context. Test services and domain logic with mocked dependencies through constructor injection.
Slice tests (part of the context):
- @WebMvcTest(OrderController.class) with MockMvc: request mapping, validation, JSON serialisation, error handlers and security rules, with services mocked via @MockitoBean (@MockBean before Boot 3.4).
- @DataJpaTest: repositories and queries against an embedded or Testcontainers database, rolled back after each test.
- @JsonTest for serialisation details.
Integration tests:
- @SpringBootTest loads the full application. Use Testcontainers for real PostgreSQL, Kafka or Redis instead of in-memory substitutes, so tests match production behaviour.
- Use @ServiceConnection (Boot 3.1+) to wire containers automatically.
- WireMock or MockRestServiceServer for external HTTP APIs.
Contract and end-to-end:
- Consumer-driven contracts (Spring Cloud Contract or Pact) between services.
- A few end-to-end tests for critical journeys only.
Speed tips: reuse the application context by keeping test configuration consistent, and avoid @DirtiesContext unless necessary.