Java Testing Spring applications
Slices, mocks and real dependencies. The judgement being tested is what you choose not to load.
3 concepts · 7 interview questions
What this topic covers
Every concept in testing spring applications, and the questions each one gets asked as. Where a question links, it has a full write-up.
Context loading and slices
@SpringBootTest starts everything; slice annotations load only one layer, which is faster and more focused.
- @SpringBootTest versus @WebMvcTest versus @DataJpaTest?
- Why is the Spring test context cached, and when is it evicted?
- When is @SpringBootTest the wrong choice?
Mocking collaborators
@MockBean replaces a bean in the context; a plain Mockito mock does not involve Spring at all.
- @Mock versus @MockBean — when does the difference matter?
- What should you not mock?
Integration testing
Testing against a real database or broker in a container, instead of an in-memory imitation that behaves differently.
- Why test against Testcontainers instead of H2?
- How do you test a transactional method's rollback behaviour?