Unit Test Articles
4 articles about Unit Test. Explore Spring Boot implementation, design, and operations across related topics.
-
How to Use JUnit 5 with Spring Boot - @DisplayName, @ParameterizedTest, @Nested Basics and Differences from JUnit 4
Learn how to use JUnit 5 (JUnit Jupiter) with Spring Boot 3.x. This guide covers how JUnit Jupiter is bundled in spring-boot-starter-test, the basics of @Test, @BeforeEach, @DisplayName, and assertThrows/assertAll, @ParameterizedTest (@ValueSource/@CsvSource/@MethodSource), and organizing test scenarios with @Nested. It also includes a mapping table from JUnit 4 (@RunWith/@Before) and checkpoints for when tests are not being executed.
-
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.