Integration Tests with @SpringBootTest
Test the whole application context.
Integration Tests with @SpringBootTest is a free Spring Boot 4 Microservices & REST APIs lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Spring Boot 4 Microservices & REST APIs learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What is an Integration Test
An integration test exercises multiple layers together — controller, service, repository — using a real Spring context.
It catches wiring and configuration problems that unit tests miss.
@SpringBootTest
@SpringBootTest boots the entire application context, loading all beans.
@SpringBootTest
class ApplicationIntegrationTest {
@Autowired
UserService userService;
@Test
void contextLoads() {
assertThat(userService).isNotNull();
}
}WebEnvironment Modes
Control whether a real server starts with the webEnvironment attribute.
MOCK(default) — mock servlet environmentRANDOM_PORT— real server on a random port
@SpringBootTest(
webEnvironment = WebEnvironment.RANDOM_PORT)
class ApiTest { }Testing with TestRestTemplate
With a real port you can make actual HTTP calls using TestRestTemplate.
@Autowired
TestRestTemplate restTemplate;
@Test
void getsUser() {
ResponseEntity<User> response =
restTemplate.getForEntity("/users/1", User.class);
assertThat(response.getStatusCode())
.isEqualTo(HttpStatus.OK);
}WebTestClient
WebTestClient is a fluent client that works for both MVC and WebFlux apps.
@Autowired
WebTestClient webTestClient;
@Test
void getsUser() {
webTestClient.get().uri("/users/1")
.exchange()
.expectStatus().isOk()
.expectBody()
.jsonPath("$.name").isEqualTo("Alice");
}Using a Test Database
By default Spring Boot can replace your datasource with an in-memory H2 database for tests.
@SpringBootTest
@AutoConfigureTestDatabase
class RepositoryIntegrationTest {
@Autowired UserRepository repository;
}Test Properties
Override configuration for tests with @TestPropertySource or an application-test.properties profile.
@SpringBootTest
@TestPropertySource(properties = {
"feature.enabled=false"
})
class FeatureTest { }Activating Profiles
Use @ActiveProfiles to run with a specific Spring profile.
@SpringBootTest
@ActiveProfiles("test")
class ServiceTest { }Transactional Rollback
Annotate tests with @Transactional so each test's database changes roll back automatically, keeping tests isolated.
@SpringBootTest
@Transactional
class UserPersistenceTest {
@Test
void savesAndRollsBack() {
repository.save(new User("Alice"));
}
}Slicing for Speed
Full @SpringBootTest is slow. Prefer slices like @DataJpaTest or @WebMvcTest when you only need one layer.
@DataJpaTest
@DataJpaTest loads only JPA components and an embedded database — ideal for repository tests.
@DataJpaTest
class UserRepositoryTest {
@Autowired UserRepository repository;
@Test
void findsByName() {
repository.save(new User("Alice"));
assertThat(repository.findByName("Alice"))
.hasSize(1);
}
}Quick Check
Test your understanding of integration testing.
Recap
You learned to write integration tests:
@SpringBootTestloads the full contextRANDOM_PORT+TestRestTemplate/WebTestClientfor real HTTP@Transactionalrolls back DB changes- Use slices for speed
Frequently asked questions
Is the “Integration Tests with @SpringBootTest” lesson free?
Yes — the full text of “Integration Tests with @SpringBootTest” is free to read here on the web, and the Spring Boot 4 Microservices & REST APIs course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Spring Boot 4 Microservices & REST APIs course, upgrade to CoddyKit PRO.
What will I learn in “Integration Tests with @SpringBootTest”?
Test the whole application context. You practise Spring Boot 4 Microservices & REST APIs with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Spring Boot 4 Microservices & REST APIs?
No prior experience is required. Spring Boot 4 Microservices & REST APIs on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Integration Tests with @SpringBootTest” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Spring Boot 4 Microservices & REST APIs lesson?
Yes. Every Spring Boot 4 Microservices & REST APIs lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Unit Testing with JUnit and Mockito
- Web Layer Tests with MockMvc
- Integration Tests with @SpringBootTest
- Real Dependencies with Testcontainers