Why Unit Tests Are Faster
Fast recall
Quick revision
Core answer
Understanding of what actually makes tests slow—context startup, I/O, network—not just code complexity.
Thinking unit tests are faster simply because they test less code or have fewer assertions.
Name the actual costs—context creation, migrations, containers, server startup, and I/O—and say that the suite should be measured rather than assigned a universal timing
Interview-ready guide
Interview answer
Unit tests are usually faster because they create a small object graph and avoid broad framework startup and external I/O. An integration test may create a Spring context, initialize pools, run migrations, start a container or server, and call a database or network service. The actual time depends on the application, machine, cache reuse, and test boundary, so I would not quote a universal threshold. I measure the test report and context-cache behavior, then keep each test at the smallest boundary that can still reveal the wiring, persistence, or protocol defect I need it to catch.
Why Unit Tests Are Faster
Direct Answer
Unit tests are usually faster because they create only the object graph needed for one behavior and avoid broad framework startup and external I/O. Integration tests may build an ApplicationContext, migrate a database, start a server, or contact containers. There is no universal timing: measure the suite, reuse cached contexts, and keep each test at the smallest boundary that proves the requirement.
Code Example
// Focused unit test: no Spring context or external I/O
@ExtendWith(MockitoExtension.class)
class OrderServiceTest {
@Mock private OrderRepository orderRepository;
@InjectMocks private OrderService orderService;
@Test
void shouldCalculateTotalWithDiscount() {
var order = new Order(1L, "Laptop", 1000.0);
when(orderRepository.findById(1L)).thenReturn(Optional.of(order));
var total = orderService.calculateTotal(1L, 0.1);
assertEquals(900.0, total);
}
}
// Integration test: context and configured persistence boundary
@DataJpaTest
class OrderRepositoryIntegrationTest {
@Autowired private OrderRepository orderRepository;
@Test
void shouldPersistOrder() {
var saved = orderRepository.save(new Order(null, "Laptop", 1000.0));
assertThat(orderRepository.findById(saved.id())).isPresent();
}
}
Measure both tests in the real build; no annotation guarantees a fixed duration.
Common Mistakes & Pitfalls
- Counting assertions instead of setup: Context creation, migrations, containers, and I/O often dominate the cost.
- Using a universal threshold: Compare with the suite's baseline and inspect setup separately from test execution.
- Using @DirtiesContext as database cleanup: It evicts a Spring context. Use a test transaction when its boundary fits, or clean database state explicitly; Testcontainers does not by itself reset data between every test.
Check your understanding
Answer these without looking back at the article.
- How does Spring Boot cache the application context between test runs?
- What is @DirtiesContext and why should you use it sparingly?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.