Mockito Articles
5 articles about Mockito. Explore Spring Boot implementation, design, and operations across related topics.
-
Mastering Mockito in Spring Boot Service Layer Tests - when, verify, ArgumentCaptor, and doThrow in Practice
Hands-on guide to Mockito's when/thenReturn, verify (times, never, inOrder), ArgumentCaptor, and doThrow/doAnswer for void methods, using OrderService tests. Also covers how to fix UnnecessaryStubbingException and InvalidUseOfMatchersException, and when to use @InjectMocks versus @MockitoBean.
-
Spring Boot Test Annotations Cheat Sheet - How to Choose Between @SpringBootTest, @WebMvcTest, and @DataJpaTest
A cheat sheet and decision flow for choosing the right Spring Boot test annotation. Covers the differences in startup scope and speed between @SpringBootTest, @WebMvcTest, @DataJpaTest, @JsonTest, and @RestClientTest, with layer-by-layer explanations of how to combine them with @MockitoBean, MockMvc, and Testcontainers.
-
@MockBean Deprecated in Spring Boot 3.4 - Migrating to @MockitoBean and @MockitoSpyBean and How to Use Them
@MockBean/@SpyBean are deprecated in Spring Boot 3.4. This guide explains the correct imports for their replacements @MockitoBean and @MockitoSpyBean, the package relocation, differences from the old API, and step-by-step procedures for migrating existing tests in bulk, with practical examples.
-
How to Write Controller Unit Tests with MockMvc in Spring Boot - Introduction to @WebMvcTest
Achieve fast, DB-free, copy-paste-ready Controller unit tests with @WebMvcTest and MockMvc. Explains how to mock Services with @MockBean, validate JSON with jsonPath, handle validation errors (400), and integrate Security (@WithMockUser) with practical examples.
-
Writing Tests in Spring Boot - An Introduction to Unit Testing with JUnit and Mockito
A step-by-step guide to writing Spring Boot unit tests with JUnit 5 and Mockito, split into the Service layer (@Mock/@InjectMocks) and the Controller layer (@WebMvcTest/@MockBean/MockMvc). Learn the Given-When-Then pattern, success and failure cases, and assertThrows through concrete code examples.