Unit vs Integration vs End-to-End Testing
Fast recall
Quick revision
Core answer
Whether you understand test scope, cost, and the testing pyramid tradeoffs
Saying you write mostly integration tests — this shows misunderstanding of cost vs confidence ratio
Mention Testcontainers for integration tests against real databases, and why you prefer real DBs over H2 in integration tests
Interview-ready guide
Interview answer
Unit tests verify one class in isolation with mocked dependencies — they run in milliseconds and tell you exactly what broke. Integration tests verify that components work together with real infrastructure — Spring context, database, HTTP. E2E tests verify complete user flows through the entire system.
The pyramid isn't about having "more unit tests" — it's about feedback speed vs confidence. A failing unit test points to the exact method in seconds. A failing E2E test says "something is broken" and takes minutes to diagnose.
The anti-pattern is the ice cream cone — mostly E2E tests, few unit tests. Tests become so slow that developers stop running them locally, and CI becomes the only feedback loop. My rule: if you can test it with a unit test, don't write an integration test. Push testing to the lowest level that still gives you confidence.
Unit vs Integration vs End-to-End Testing
Direct Answer
Unit tests verify individual methods/classes in isolation using mocks, run in milliseconds. Integration tests verify components working together (e.g., controller + service + real DB), run in seconds. E2E tests verify full user journeys through the deployed application, run in minutes. The testing pyramid recommends many unit tests, fewer integration tests, and very few E2E tests.
Common Mistakes & Pitfalls
Saying you write mostly integration tests — this shows misunderstanding of cost vs confidence ratio
Check your understanding
Answer these without looking back at the article.
- How do you decide what to mock vs use real implementations?
- What's your experience with Testcontainers?
- How do you handle flaky tests?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.