Unit Test Articles
3 articles about Unit Test. Explore Spring Boot implementation, design, and operations across related topics.
-
How to Test @RestControllerAdvice Exception Handlers Using MockMvc in Spring Boot - Verifying Status Codes and ProblemDetail
Using @WebMvcTest and MockMvc, this article explains implementation patterns for verifying that a GlobalExceptionHandler (@RestControllerAdvice) correctly converts exceptions thrown from the Service layer into the proper HTTP status codes and JSON bodies (ProblemDetail or custom error responses).
-
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.
-
Let's Write Tests in Spring Boot - An Introduction to Unit Testing with JUnit and Mockito
A step-by-step guide to writing unit tests for Spring Boot with JUnit 5 and Mockito, covering the Service layer (@Mock/@InjectMocks) and the Controller layer (@WebMvcTest/@MockBean/MockMvc) separately. Learn through concrete code examples including the Given-When-Then pattern, normal and abnormal cases, and assertThrows.