How do you write integration tests in Spring Boot with @SpringBootTest?
Learn to write Spring Boot integration tests with @SpringBootTest: webEnvironment options, MockMvc, TestRestTemplate, @MockBean, and clean test data setup.
Expected Interview Answer
You annotate a test class with @SpringBootTest, which boots the full Spring application context so your beans, configuration, and wiring are exercised together rather than in isolation, and then you inject and drive real collaborators to verify end-to-end behavior.
@SpringBootTest can start the context with a mock environment or a real embedded server via webEnvironment (MOCK, RANDOM_PORT, DEFINED_PORT, NONE). For web layers you combine it with MockMvc or TestRestTemplate/WebTestClient, and for the database you use a real or in-memory instance often managed with Testcontainers and @Sql seed data. @Transactional rolls back changes after each test, @MockBean replaces specific collaborators, and @ActiveProfiles selects test configuration.
- Verifies that beans wire together as they do in production
- Catches configuration and serialization bugs unit tests miss
- Supports real HTTP calls via RANDOM_PORT and TestRestTemplate
- Allows selective stubbing of collaborators with @MockBean
- Rolls back database state automatically with @Transactional
- Reuses production-like infrastructure through Testcontainers
AI Mentor Explanation
A unit test is net practice where one batter faces a bowling machine in isolation. An integration test with @SpringBootTest is a full practice match with real bowlers, fielders, umpires, and a scoreboard, so you find out whether the whole side actually functions together under match conditions rather than testing one skill in a sealed net.
Step-by-Step Explanation
Step 1
Annotate the test class
Add @SpringBootTest so the full application context loads for the test.
Step 2
Pick a web environment
Use webEnvironment = RANDOM_PORT for real HTTP tests or MOCK with @AutoConfigureMockMvc for the servlet layer.
Step 3
Inject collaborators
Autowire real beans like repositories and services, or inject MockMvc/TestRestTemplate to drive the app.
Step 4
Provision infrastructure
Use Testcontainers or an in-memory database, select config with @ActiveProfiles, and seed with @Sql.
Step 5
Isolate and assert
Stub specific beans with @MockBean, wrap in @Transactional for rollback, then assert responses and persisted state.
What Interviewer Expects
- Knowing @SpringBootTest loads the full context
- Understanding the webEnvironment options
- Using MockMvc, TestRestTemplate, or WebTestClient appropriately
- Managing test data and rollback with @Transactional and @Sql
- Difference between @MockBean and a plain Mockito mock
- Awareness of slice tests like @WebMvcTest and @DataJpaTest
Common Mistakes
- Using @SpringBootTest for everything when a slice test would be faster
- Forgetting to set webEnvironment for real HTTP tests
- Not isolating external systems, causing flaky tests
- Leaking state between tests without rollback or cleanup
- Confusing @MockBean with @Mock from plain Mockito
Best Answer (HR Friendly)
“Integration tests start up the whole Spring Boot application in a test and check that all the pieces work together, not just one method. You use @SpringBootTest to load everything, then send real requests and confirm the app responds and stores data correctly.”
Code Example
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("test")
class OrderControllerIT {
@Autowired
private TestRestTemplate restTemplate;
@MockBean
private PaymentGateway paymentGateway;
@Test
void createsOrderAndReturns201() {
var request = new OrderRequest("SKU-1", 2);
ResponseEntity<OrderResponse> response =
restTemplate.postForEntity("/orders", request, OrderResponse.class);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.CREATED);
assertThat(response.getBody().id()).isNotNull();
}
}Follow-up Questions
- When would you use @WebMvcTest or @DataJpaTest instead of @SpringBootTest?
- What is the difference between @MockBean and @Mock?
- How do you test against a real database with Testcontainers?
- How does webEnvironment = RANDOM_PORT differ from MOCK?
- How do you keep integration tests isolated and repeatable?
MCQ Practice
1. What does @SpringBootTest primarily do?
@SpringBootTest bootstraps the entire Spring application context, exercising real bean wiring and configuration.
2. Which webEnvironment starts a real embedded server on a random port?
RANDOM_PORT starts a real servlet container on an available port, ideal for TestRestTemplate-based HTTP tests.
3. What does @MockBean do in a @SpringBootTest?
@MockBean adds or replaces a bean in the Spring context with a Mockito mock so collaborators can be stubbed.
Flash Cards
What does @SpringBootTest load? — The full Spring application context for end-to-end testing.
webEnvironment for real HTTP tests? — RANDOM_PORT (or DEFINED_PORT) with TestRestTemplate or WebTestClient.
@MockBean vs @Mock? — @MockBean replaces a bean inside the Spring context; @Mock is a standalone Mockito mock.
How to roll back DB changes per test? — Annotate with @Transactional so each test rolls back after completion.
Faster alternative to full context tests? — Slice tests like @WebMvcTest and @DataJpaTest load only part of the context.
Continue Learning
Related Interview Questions
Your Spring Boot test suite has become very slow — how does test context caching work and how do you fix it?
hard
How do you decide between @WebMvcTest, @DataJpaTest and a full @SpringBootTest?
medium
How do you run integration tests against real infrastructure without making the build slow or flaky?
hard
What is @ConfigurationProperties and how does it bind external configuration?
medium