Testing Articles
Articles covering unit testing, integration testing, and Testcontainers usage for Spring Boot test strategies.
-
@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 to Detect Configuration Errors at Startup
Learn how to combine Spring Boot's @ConfigurationProperties with Bean Validation (@Validated/@NotBlank/@Pattern) to instantly detect configuration mistakes at application startup before they cause production incidents. Covers @Valid propagation for nested validation, 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.
-
Writing Tests in Spring Boot - Introduction to Unit Testing with JUnit and Mockito
Learn how to write Spring Boot unit tests with JUnit 5 and Mockito, explained step by step for the Service layer (@Mock/@InjectMocks) and Controller layer (@WebMvcTest/@MockBean/MockMvc). Master the Given-When-Then pattern, success/failure cases, and assertThrows through concrete code examples.