Integration Testing
Write Spring Boot integration tests with @SpringBootTest, Testcontainers, and @DataJpaTest — testing real component interactions with actual databases.
Last reviewed: January 2025
🔍 What Is It?
Integration tests verify that multiple components work together correctly.
Spring Boot test slices:
- →
@SpringBootTest— full application context (all beans) - →
@WebMvcTest— web layer only (controllers, filters) - →
@DataJpaTest— JPA layer only (repositories, entities) - →
@DataRedisTest— Redis layer only - →
@RestClientTest— REST client testing
Test support:
- →
@MockBean— replace a bean with a Mockito mock in the context - →
@SpyBean— wrap a real bean with a Mockito spy - →
@Sql— execute SQL before/after tests - →
@Transactionalon test class — rolls back after each test - →
Testcontainers— real databases in Docker
🎯 Why Do We Need It?
Unit tests can't catch integration bugs:
- →Wrong SQL query in @Query annotation
- →Transaction propagation issues
- →Security configuration blocking valid requests
- →Entity mapping problems
- →Auto-configuration conflicts
Integration tests catch these by running real components together.
🏗️ What Happens in Real Projects?
Integration test strategy:
- →Repository tests with @DataJpaTest + H2 or Testcontainers
- →Controller tests with @WebMvcTest + @MockBean for services
- →Full flow tests with @SpringBootTest for critical paths
- →Security tests verifying authentication and authorization
⚙️ How It Works
@SpringBootTest creates an ApplicationContext with all beans. @DataJpaTest creates a minimal context with only JPA-related beans and an embedded database. Test slices are much faster than full @SpringBootTest because they load fewer beans.
💻 Example
Integration Testing Patterns — @DataJpaTest, @WebMvcTest, and Testcontainers.
🤔 Common Confusions
❓ When should I use @SpringBootTest vs @WebMvcTest?
@WebMvcTest is faster — it only loads the web layer (controllers, filters, security). Use it for controller tests. @SpringBootTest loads everything — use it for full integration tests that need the complete application context (e.g., testing a full request flow through controller → service → repository).
❓ Should I use H2 or Testcontainers for database tests?
Testcontainers with the real database (PostgreSQL, MySQL). H2 is a different database — it doesn't support all PostgreSQL features (JSONB, window functions, specific constraints). Tests that pass with H2 can fail with real PostgreSQL. Testcontainers adds startup time but gives you confidence that tests reflect production behavior.
🚨 Common Production Issues
@SpringBootTest used for all tests — makes the test suite take minutes instead of seconds
Tests sharing state via @Autowired repositories without @Transactional rollback
Testcontainers starting a new container per test class — use static containers for reuse
✅ Best Practices
Use test slices (@WebMvcTest, @DataJpaTest) instead of @SpringBootTest when possible
Use Testcontainers for repository tests — test against the real database
Add @Transactional to test classes to auto-rollback after each test
Use @Sql to set up test data declaratively
Share Testcontainers instances across test classes with static containers
🏛️ Architect's Note
The test suite performance matters. A suite that takes 30 minutes to run means developers avoid running it. Target: unit tests < 30 seconds, integration tests < 5 minutes. Use test slices aggressively. Share Testcontainers instances with the Singleton pattern (static container + @DynamicPropertySource). Spring Boot 3.2+ supports Testcontainers auto-configuration — define containers in application.properties.
💬 Frequently Asked Questions
🎯 Key Takeaways
🔗 Related Topics
🎯 Practice this topic in interviews
↗ Where this topic fits
Spring Boot Technology Guide
Return to the Spring Boot learning hub.
Backend Engineer Career Path
A structured journey through Core Java, Spring Boot, production backend engineering, and interview preparation.
Full Stack Java Developer Career Path
A guided route through frontend foundations, Java backend engineering, delivery workflows, and interview readiness.
Sources
Sources / References
Use these Spring Boot references to verify the concept in official docs and continue into the most relevant SplashRide pages.
Official documentation
Related SplashRide pages
SplashRide Spring Boot learning path
Browse the full Spring Boot topic collection and learning order on SplashRide.
Spring Boot Interview Prep
Practice Spring Boot interview questions and scenario-based answers.
Backend Engineer Career Path
A structured journey through Core Java, Spring Boot, production backend engineering, and interview preparation.
Full Stack Java Developer Career Path
A guided route through frontend foundations, Java backend engineering, delivery workflows, and interview readiness.
Unit Testing
Write effective unit tests for Spring Boot services using JUnit 5, Mockito, and AssertJ — fast, isolated tests that don't start a Spring context.
MockMvc
Test Spring MVC controllers with MockMvc — the fluent API for testing HTTP request handling, response status, JSON content, and security without starting a real server.