Mockmvc Articles
7 articles about Mockmvc. Explore Spring Boot implementation, design, and operations across related topics.
-
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 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 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).
-
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 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()).
-
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.
-
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.