Unit Testing Best Practices — Naming, Arrange-Act-Assert, Test Isolation
Fast recall
Quick revision
Core answer
Whether the candidate understands test design principles that make tests maintainable, readable, and reliable.
Tests that depend on execution order or shared mutable state — they pass individually but fail in suite.
Explain Arrange-Act-Assert structure, meaningful test names, test isolation through dependency injection, and the difference between testing behavior vs implementation.
Interview-ready guide
Interview answer
Good tests follow three principles. Arrange-Act-Assert gives each test a clear structure — setup, execution, verification. Test isolation means each test runs independently with no shared mutable state — @BeforeEach initializes everything fresh. Behavior over implementation means testing what the code does, not how it does it.
Naming matters more than people think. shouldRejectOrderWhenInventoryInsufficient tells you what broke without reading the test. testMethod1 tells you nothing.
The one logical assertion guideline: each test verifies one behavior. Multiple assertThat calls are fine if they verify the same behavior. But discount AND notification AND logging in one test means three behaviors — split into three tests so failures are immediately diagnosable.
Flaky tests almost always come from shared mutable state. Fresh mocks, fresh data, fresh service instances per test — that's isolation.
Unit Testing Best Practices — Naming, Arrange-Act-Assert, Test Isolation
Direct Answer
Good unit tests follow Arrange-Act-Assert, have descriptive names, are isolated from each other, and test behavior rather than implementation details.
Common Mistakes & Pitfalls
Tests that depend on execution order or shared mutable state — they pass individually but fail in suite.
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.