Supply the expected behaviour
Describe the unit’s contract before showing the proposed implementation change. Include a normal example, an invalid input and a boundary. State whether errors are returned or raised and which external effects are outside this unit.
Provide the actual language and framework versions and the command the repository uses. A generated example that imports a plausible but nonexistent API adds work without protecting behaviour. Keep generated test files small enough to review.
Review assertions and doubles
Check that the expected value comes from the specification or a worked example. A test that computes its expected value by calling the same helper as the implementation can repeat the defect.
Use a double for a real external boundary, not every internal function. Over-mocking can prove that the test and implementation share a call sequence while missing the wrong returned value. Keep at least one direct assertion on the observable outcome.
Prove the test can detect the defect
Run the generated test on the baseline, then introduce a small wrong result in a disposable copy. Confirm failure for the intended reason. Restore the candidate immediately; do not commit the mutation.
A coverage increase is a useful observation but is not an acceptance criterion by itself. Retain the test command, the passing output and the demonstrated failure case with the patch review.
Working sheet
| Review question | Concrete check | Reject when |
|---|---|---|
| Does it assert the contract? | Check the result for a known input | It only checks a mock was called |
| Is the expected value independent? | Work through the example separately | Expected value calls the same helper |
| Is the boundary covered? | Try just below, at and above threshold | Only one convenient input exists |
| Can it fail usefully? | Deliberately change the result locally | The test still passes |
Example to adapt
Illustrative example. Run it against your own fixtures and supported tool versions.
def shipping_cost(total):
return 0 if total >= 50 else 5
def test_free_shipping_boundary():
assert shipping_cost(49) == 5
assert shipping_cost(50) == 0Generate Java unit tests with AI
Provide the Java version, the test framework and the build command. Ask for a test of public behaviour using one concrete input and expected result, rather than a mock-heavy copy of the implementation.
Run the generated test against the original code and against a deliberately incorrect result. Check that it fails for the intended reason. Restore the code immediately after that local mutation experiment.
Questions about aI unit test generation that catches mistakes
Should I let the agent write tests and code together?
It can draft both, but review them independently. Otherwise the same mistaken assumption can appear in the implementation and the expected assertion.
Do generated tests replace integration tests?
No. Isolated tests cannot prove that routing, persistence or external services are wired correctly. Use integration checks for the boundaries your change actually crosses.
How should I use the working sheet?
Download the CSV and replace the sample rows with evidence from your repository. Record a clear acceptance check and an owner for each item. Keep unresolved assumptions visible rather than treating the example as a completed assessment.
Sources and further reading
Examples are illustrative. They are not customer results, security guarantees, or a substitute for checking the documentation for your installed version.
