Chapter 1 · Practice 4 of 4
Spend tests on logic and risk, not on trivial code
Why it matters
Not every line deserves a test. Getters, setters and plain data mappings with no branches add maintenance cost and very little safety. Fowler warns against testing trivial code just to raise a coverage number.
Spend your effort where bugs actually live:
- Branches and conditions: every
if,switchand early return - Boundaries: empty lists, zero, the maximum allowed value, one past it
- Error paths: invalid input, a dependency that times out, a missing record
- Money, permissions and data loss: anything where a bug is expensive
When you are unsure whether something needs a test, ask what would happen if it broke silently. If the answer is "a user loses data" or "someone sees another customer's invoice", it needs a test. If the answer is "a label is slightly wrong", it may not.
Do
- List the branches and boundaries of a function before writing its tests
Avoid
- Tests that only call a getter and check it returns what a setter stored