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