Testing Articles
Articles covering unit testing, integration testing, and Testcontainers usage for Spring Boot test strategies.
-
How to Test File Upload APIs Using MockMvc in Spring Boot - Testing Error Cases for Size Limits and MIME Validation with MockMultipartFile
Learn how to use MockMultipartFile and perform(multipart()) to test file upload APIs, covering not only normal-case testing but also how to write error-case tests that verify a 400 response is returned when the size limit is exceeded or an invalid MIME type is used.
-
How to Unit Test Mappers in Spring Boot + MyBatis - Why @MybatisTest Doesn't Exist and Alternative Configurations with H2/Testcontainers
MyBatis Mapper has no dedicated slice test equivalent to @DataJpaTest. This article explains why, based on the coupling with SqlSession, and compares four patterns—minimal @SpringBootTest configuration, lightweight Java Config, H2, and Testcontainers—with a comparison table and working code.
-
How to Test File Download APIs with MockMvc in Spring Boot - Verifying Content-Disposition and Binary Responses
Explains how to test file download APIs with MockMvc (downloads after saving a MultipartFile, StreamingResponseBody, Excel/PDF/CSV exports). Covers verifying the Content-Disposition header, matching binary bodies, and testing patterns for Range requests (206 Partial Content), with implementation examples.
-
How to Load Test Data in Spring Boot Tests Using the @Sql Annotation
Learn how to use the @Sql annotation with @DataJpaTest and @SpringBootTest to declaratively load fixture data for each test. Covers how to use executionPhase and errorMode, the difference from data.sql, and more, with practical examples.
-
Spring Boot @SpringBootTest webEnvironment Guide - Choosing Between MOCK, RANDOM_PORT, DEFINED_PORT, and NONE
A comparison table and working code clarify what each of the four @SpringBootTest webEnvironment modes (MOCK/RANDOM_PORT/DEFINED_PORT/NONE) does and does not start up. Covers replacing external dependencies with @MockitoBean, splitting scenarios with @Nested, and the cases where launching the full ApplicationContext is truly necessary.
-
How to Test JSON Serialization/Deserialization in Spring Boot with @JsonTest - Validating JacksonTester and ObjectMapper
Learn how to lightly and quickly test DTO serialization/deserialization specifications using the @JsonTest slice and JacksonTester. This guide covers implementation examples for date formatting, null-exclusion settings, and regression prevention for custom Serializers/Deserializers with @JsonComponent.
-
How to Test Authentication-Required APIs with Spring Security and MockMvc - Choosing Between @WithMockUser and SecurityMockMvcRequestPostProcessors
Explains why testing a Controller that has Spring Security enabled with @WebMvcTest fails with 401/403, and introduces implementation approaches for reproducing an authenticated state by choosing between @WithMockUser, @WithUserDetails, and SecurityMockMvcRequestPostProcessors (user()/csrf()/authentication()).
-
How to Write Real HTTP E2E Tests in Spring Boot with @SpringBootTest(RANDOM_PORT) and TestRestTemplate
Learn how to actually launch your application with @SpringBootTest(webEnvironment = RANDOM_PORT) and write integration tests that verify status codes, headers, and bodies using TestRestTemplate. Covers the differences from MockMvc, sending requests to authenticated endpoints, and validating error responses, all with working code examples.
-
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 Repository Layer Slice Tests with @DataJpaTest in Spring Boot - Choosing Between H2 and Testcontainers
A guide to writing slice tests focused on the Repository layer using @DataJpaTest in Spring Boot 3.x. Covers automatic rollback, TestEntityManager, switching between H2 and a real database via @AutoConfigureTestDatabase, verifying query methods and @Query, and when to combine Testcontainers — all 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.
-
How to Validate Spring Boot @ConfigurationProperties with Bean Validation - A Fail Fast Implementation Guide for Detecting Configuration Errors at Startup
Learn how to combine Spring Boot @ConfigurationProperties with Bean Validation (@Validated/@NotBlank/@Pattern) to catch configuration errors at application startup, before they become production incidents. Covers nested validation with @Valid propagation, how to read startup error messages, and lightweight testing with ApplicationContextRunner, all with implementation examples.
-
How to Test External API Calls with WireMock in Spring Boot - From import Setup to Practice
A practical guide to introducing WireMock into Spring Boot tests and defining external API stubs at the HTTP level, covering success cases, error cases, and timeouts. Also explains the correct import statement for WireMock 3.x's new package org.wiremock.client.WireMock, as well as criteria for choosing between Mockito and WireMock.
-
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.