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