@SpringBootTest Purpose and Usage
Fast recall
Quick revision
Core answer
Whether you understand that @SpringBootTest boots the whole context and know the cost trade-off versus slice tests and unit tests
Using @SpringBootTest for isolated logic that could be tested without starting a Spring context
Mention specific webEnvironment modes and explain you prefer @WebMvcTest or @DataJpaTest for slice testing when possible
Interview-ready guide
Interview answer
@SpringBootTest creates a Boot application context for an integration test. It can find the main Boot configuration and apply auto-configuration, so it is useful when several real application layers or Boot's wiring are part of the behavior being checked. What actually starts still depends on the test's profiles, properties, mocks, exclusions, and webEnvironment, so it is not automatically identical to production. The default MOCK mode does not open a real HTTP port; RANDOM_PORT does. Because a broad context has more setup than a unit test or slice, I use the smallest test boundary that can prove the behavior. Spring can cache compatible contexts, so unnecessary configuration differences and @DirtiesContext reduce reuse.
@SpringBootTest Purpose and Usage
Direct Answer
@SpringBootTest asks Spring Boot to create an ApplicationContext through SpringApplication for an integration test. It can discover the main Boot configuration and apply auto-configuration, but the beans and external connections actually created still depend on profiles, properties, mocks, exclusions, and webEnvironment. Use it when the behavior needs broad Boot wiring; use a plain unit test or a slice when that smaller boundary is enough.
Code Example
@SpringBootTest
@Transactional
class OrderServiceIntegrationTest {
@Autowired
private OrderService orderService;
@Autowired
private OrderRepository orderRepository;
@Test
void shouldCreateOrderAndPersistIt() {
var order = new Order(null, "Laptop", 1200.00);
var savedOrder = orderService.createOrder(order);
assertThat(savedOrder.id()).isNotNull();
assertThat(orderRepository.findById(savedOrder.id()))
.isPresent()
.get()
.extracting(Order::item)
.isEqualTo("Laptop");
}
}
This verifies the real service-to-repository path present in this test context. @Transactional rolls back database work performed in the test-managed transaction.
Common Mistakes & Pitfalls
- Using @SpringBootTest for every test: A full Boot context is unnecessary for a pure function or a class whose collaborators can be supplied directly.
- Confusing MOCK with a running server: The default mode does not open a listening HTTP port. Use RANDOM_PORT or DEFINED_PORT for a real socket.
- Assuming automatic cleanup: Database changes are not rolled back merely because a test uses @SpringBootTest. Use a test-managed transaction where appropriate, or clean state explicitly. Remember that work committed in another thread, such as a real HTTP server request, is outside the test method's transaction.
Check your understanding
Answer these without looking back at the article.
- What is the difference between @SpringBootTest and @WebMvcTest?
- How does Spring Boot find the main class when running @SpringBootTest?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.