Unit vs Integration vs E2E Tests | Java Fresher Interview
Fast recall
Quick revision
Core answer
Interview-ready guide
Interview answer
A unit test checks a small behavior through a narrow boundary. For a shipping rule, I can pass an order and destination directly and verify the charge. Its collaborators do not all have to be mocks; simple real objects or a small fake may be clearer.
An integration test checks a collaboration that the unit test deliberately leaves out. For example, a repository test can use the target database engine to verify SQL, mappings, constraints, and transaction behavior. An end-to-end test goes wider and follows a user-visible path such as checkout through the assembled application. It gives confidence in wiring but usually takes more setup and gives a broader failure to diagnose.
The test pyramid is a useful heuristic rather than a quota. Put detailed rule cases at a narrow level, integration risks at the boundary they involve, and broad tests around valuable journeys. The right mix depends on architecture, risk, and feedback cost.
Unit vs Integration vs E2E Tests | Java Fresher Interview
Direct Answer
Unit tests check a small behavior through a narrow boundary and may use simple real collaborators or test doubles. Integration tests check selected components or infrastructure together, such as repository code with a database. End-to-end tests exercise a broad user-visible workflow across the assembled system. The test pyramid suggests placing more detailed checks at narrower, cheaper levels, but it does not set a universal ratio.
Check your understanding
Answer these without looking back at the article.
- What is @SpringBootTest in Spring and when do you use it?
- What is @DataJpaTest used for?
- What is the difference between a test double and a mock?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.