Good Unit Test Characteristics | Java Fresher Interview
Fast recall
Quick revision
Core answer
Interview-ready guide
Interview answer
FIRST stands for Fast, Independent, Repeatable, Self-validating, and Timely. Fast means the feedback is quick enough for the suite's purpose, not that every test must meet one timing number. Independent means a test owns its starting state and does not require another test to run first. Repeatable means time, randomness, I/O, and environment are controlled enough to reproduce a result. Self-validating means assertions or verification decide pass or fail. Timely means the test is added close to the behavior change.
A good test also has a name that explains the rule and one coherent reason to fail. For example, appliesDiscountForPremiumCustomer prepares a premium customer, calls the pricing method, and checks the final price. Separate inventory or email behavior belongs in focused tests. Avoid asserting incidental internal calls unless those interactions are the contract.
Good Unit Test Characteristics | Java Fresher Interview
Direct Answer
A good unit test gives quick feedback, runs independently, controls variable inputs, and decides pass or fail from observable behavior. FIRST summarizes this as Fast, Independent, Repeatable, Self-validating, and Timely. There is no universal millisecond limit. Clear names and a simple Arrange-Act-Assert or Given-When-Then flow usually make the test easier to understand.
Common Mistakes & Pitfalls
- Order-dependent tests. Static fields or a shared DB that accumulates state; test 3 passes only if test 1 ran first. Breaks parallel runs.
- Multiple behaviors per test. Checking discount, inventory, and email in one method; a failure doesn't tell you which broke.
- Vague names.
testServiceortestCase1say nothing; prefershouldRejectOrderWhenInventoryInsufficient. - Brittle assertions. Asserting exact string formatting or internal calls makes refactors break tests even when behavior is unchanged.
Check your understanding
Answer these without looking back at the article.
- What is the Arrange-Act-Assert pattern?
- Why should tests not test private methods?
- What is flaky test and how do you prevent it?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.