Clean • Professional
Integration Testing in Spring Boot ensures that different parts of your application work together correctly. Unlike unit tests that check individual classes, integration tests verify end-to-end behavior within the real Spring context and infrastructure (or controlled substitutes).
It helps confirm that your app behaves like it will in production, catching real-world issues before they reach users.
Integration Testing checks whether multiple components of your Spring Boot application work correctly together.
It validates:

Integration tests ensure your application is tested as a complete working system, not just isolated classes.
Most real bugs happen between components, not inside a single class. Integration testing helps to:
Unit testing checks individual classes in isolation with mocks, making it fast. Integration testing checks multiple components together with the real Spring context and database for realistic behavior.
| Feature | Unit Testing | Integration Testing |
|---|---|---|
| Scope | Single class | Multiple components |
| Dependencies | Mocked | Real (or partially mocked) |
| Database | Not tested | Tested with real or in-memory DB |
| Spring Context | Not loaded | Fully loaded |
| HTTP Calls | Not tested | Tested end-to-end |
| Speed | Very fast | Medium (slower due to context & DB) |
| Purpose | Validate logic in isolation | Validate interactions & full flow |
| Tool | Purpose |
|---|---|
@SpringBootTest | Loads the full Spring application context |
@AutoConfigureMockMvc | Enables REST API testing |
| MockMvc / WebTestClient | Perform HTTP request testing |
| Testcontainers | Run real databases or services in Docker |
| WireMock | Mock external HTTP APIs |
| H2 / PostgreSQL | Database integration testing |
| Embedded Kafka | Kafka messaging tests |
| RestAssured | Fluent and readable API testing |
@SpringBootTest@SpringBootTest loads the full Spring application context, making tests behave like production.
@SpringBootTest
classApplicationIT { }
@SpringBootTest
classContextLoadTest {
@Test
voidcontextLoads() { }
}
application-test.yml)Use test-specific profiles to isolate test configuration from production.
spring:
datasource:
url:jdbc:h2:mem:testdb
@ActiveProfiles("test")
@SpringBootTest
classUserServiceIT { }
Integration testing with databases ensures that your data layer works correctly with the application. It verifies repository queries, transactions, relationships, and persistence in a realistic environment.
| Aspect | H2 (In-Memory) | Real Database (PostgreSQL/MySQL) |
|---|---|---|
| Speed | Very fast | Slower than H2 |
| Environment | In-memory, ephemeral | Real SQL engine, production-like |
| Accuracy | Less accurate, may not match all SQL behavior | Fully accurate, matches production behavior |
| Use Case | Quick unit-level or integration tests where exact SQL behavior isn’t critical | Critical integration tests, transaction validation, and production-like queries |
Prefer Testcontainers for critical database paths.
@DataJpaTest@DataJpaTest is used for repository-focused integration testing with the database.
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
@DataJpaTest
class UserRepositoryIT {
@Autowired
private UserRepository repository;
@Test
void shouldSaveUser() {
// Create a new User entity
User user = new User("Alice");
// Save entity to database
User saved = repository.save(user);
// Verify the entity was persisted
assertNotNull(saved.getId());
}
}
Use @Transactional in Spring Boot tests to automatically roll back database changes after each test. This keeps tests isolated and the database clean.
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.transaction.annotation.Transactional;
@SpringBootTest
@Transactional
class OrderIT {
@Test
void shouldCreateOrder() {
// Example: create and save an order
Order order = new Order("Order001");
Order saved = orderRepository.save(order);
// Verify it is persisted
assertNotNull(saved.getId());
}
}
Integration tests can validate:
@SpringBootTest + MockMvc
@SpringBootTest
@AutoConfigureMockMvc
classUserControllerIT {
@Autowired
private MockMvc mockMvc;
@Test
voidshouldReturnUser()throws Exception {
mockMvc.perform(get("/users/1"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id").value(1));
}
}
Key Validations
Tests business logic + repository together without mocks. Ensures transactions, validation rules, and error handling work end-to-end.
Service + Repository Together
@SpringBootTest
classPaymentServiceIT {
@Autowired
private PaymentService service;
@Test
voidshouldProcessPayment() {
Paymentresult= service.process(100);
assertEquals("SUCCESS", result.getStatus());
}
}
@Transactional in Service Tests
End-to-End Business Logic Testing
Integration tests verify:
Testcontainers lets you run real infrastructure in Docker during integration tests, giving production-like behavior without affecting shared environments.
Supports:
@Testcontainers
@SpringBootTest
classOrderRepositoryIT {
@Container
static PostgreSQLContainer<?> postgres =newPostgreSQLContainer<>("postgres:15");
}
@DynamicPropertySource
staticvoidproperties(DynamicPropertyRegistry registry) {
registry.add("spring.datasource.url", postgres::getJdbcUrl);
}
@ActiveProfiles
Use @ActiveProfiles to load environment-specific configurations during tests.
@ActiveProfiles("test")
@SpringBootTest
classConfigIT { }
Conditional Beans with Profiles
@Profile("test")
@Bean
DataSourcetestDataSource() { ... }
Context Loading Failures
Slow Tests
@SpringBootTest or heavy Docker/Testcontainers setups.Data Leakage Between Tests
Flaky Tests
When NOT to Use Integration Tests
Use unit tests for faster and isolated testing.
Naming Conventions
UserServiceIT, OrderControllerIT, PaymentRepositoryIT for easier identification.Parallel Execution Tips
Integration Testing in Spring Boot ensures your app works as a whole, not just in parts.
It checks:
Benefits: catch real bugs, boost confidence, stabilize releases, complement unit & E2E tests.