Java with AI - Tip #2 - Generate tests before implementation
Published on
Tip: Use AI to generate tests before implementation
When using AI in writing code, tests should come first, even before writing the actual implementation.
AI is excellent at generating test cases if we define the expected behavior clearly, and creating tests first helps clarify requirements, reduce ambiguity, and catch edge cases early.
By generating tests before implementation, a contract is established on what the code should do.
There are a few reasons why I feel this is good idea:
1. AI works best when expected behavior is explicit
If you ask an AI model to generate code without clear criteria, it can produce correct-looking but incorrect behavior. By creating tests first, you:
- define exact expectations;
- reduce misunderstanding in prompts;
- make sure edge cases are addressed.
This is similar to Test-Driven Development, but AI accelerates the process. You prompt the model to produce test cases and implementations, but starting with tests gives you that extra clarity that, in many cases, is needed.
2. Tests as contracts for AI
A test suite defines how the code must behave, not just what it should look like. When tests describe behavior explicitly:
- AI outputs are easier to validate;
- implementation becomes simpler;
- failures show you exactly what needs to be fixed.
This is similar to contract-first API design, where you define the interface before the implementation. Tests serve as that contract, clearly specifying inputs, outputs, and expected behaviors. This gives AI a precise target to hit.
AI can generate tests for both positive and negative scenarios, helping to improve code quality from the outset.
A test-first workflow with AI
Here’s a practical workflow that can be used:
Define the mandatory behavior
Write a short description or list of requirements.Ask AI to generate tests based on that description
For example:Generate unit tests for a method that parses a date string in format "YYYY-MM-DD" and throws an exception for invalid formats.Review and refine the generated tests
Make sure they exactly match expectations.Ask AI to generate the implementation that satisfies the tests
With tests in place, AI has a clear contract to meet.Run the tests locally
Fix any mismatches, and reiterate.
Yet another word of caution: There’s a subtle irony in using AI to generate tests that will validate AI generated code. The tests themselves need careful human review as AI might still miss edge cases, make incorrect assumptions about expected behavior, or generate tests that pass trivially. Step 3 (review and refine) is not optional!
Benefits of test-first with AI
Clarity and precision
Tests force you and the AI to think about:
- input and output;
- edge cases;
- error handling;
- performance assumptions.
Without tests, assumptions remain implicit and AI might guess incorrectly.
Better code quality
AI generated implementations that satisfy tests are more robust because the tests define clear success criteria.
Faster debugging
If a test fails, you know exactly what behavior is incorrect and not just that the code doesn’t compile or run.
Easy refactoring
With a solid test suite, you can easily regenerate or refactor code using AI without fear of breaking behavior.
Example
Suppose you need a method that validates email addresses.
You ask AI:
Generate unit tests for a method "boolean isValidEmail(String email)" that returns true for valid emails and false otherwise.AI may produces tests like:
@Test public void testIsValidEmailMustReturnTrue() { assertTrue(isValidEmail("[email protected]")); assertTrue(isValidEmail("[email protected]")); } @Test public void testIsValidEmailMustReturnFalse() { assertFalse(isValidEmail("plainaddress")); assertFalse(isValidEmail("missing@domain")); }Review and refine the tests for edge cases (e.g.,
null, empty).Generate implementation code with AI that passes all generated tests.
This approach turns ambiguity into a clear behavior specification and boosts confidence in AI outputs.

This work is licensed under a Creative Commons Attribuition-ShareAlike 4.0 International License .
