Who Tests the Tests? Mutation Testing When AI Writes Your Unit Tests
AI tools can take a service from 20% to 95% line coverage in an afternoon. Coverage only tells you the code ran, not that a test would notice if it broke. Mutation testing breaks your code on purpose and checks whether the tests complain. Here is how it works, how to run it on a Java service with PIT, and how to use it as the honesty check for AI-generated test suites.
- #testing
- #java
- #ai
- #code-quality
- #ci-cd
Here's a test that passes, raises your coverage, and protects you from absolutely nothing:
@Test
void calculatesDiscount() {
discountService.calculate(order);
}No assertion. The method ran, every line inside it turned green in the coverage report, and if calculate started returning negative prices tomorrow this test would keep passing with a calm, untroubled face.
Nobody writes that test on purpose. But AI tools write tests very fast, in very large numbers, and a percentage of them come out like this: they execute code, check something trivial (assertNotNull(result) is a favourite), and move on. At five tests a day you'd spot them in review. At five hundred in an afternoon you won't.
Coverage was always a weak signal. AI-generated tests make it a misleading one. The tool that fixes this has existed for years and most teams have never run it: mutation testing.
The idea in one paragraph
Mutation testing makes small, deliberate bugs in your code, one at a time, and runs your tests against each one. If a test fails, the bug (the "mutant") was killed. Good. If every test still passes, the mutant survived, which means your tests would not have noticed that bug in real life. The percentage of mutants killed is your mutation score.
It's a fire drill for your test suite. Coverage tells you the smoke detectors are installed. Mutation testing lights a match under each one.
What a mutant looks like
Take this method:
boolean isEligible(Customer c) {
return c.age() >= 18 && c.creditScore() > 650;
}A mutation tool will produce variants like:
c.age() > 18(boundary changed)c.age() < 18(condition negated)c.creditScore() >= 650(boundary changed)return true;(return value replaced)return false;
Now ask: does any test fail when >= becomes >? Only if some test checks a customer who is exactly 18. If your suite tests a 25-year-old and a 12-year-old, that mutant survives, and the bug where 18-year-olds get rejected ships quietly.
That's the kind of gap coverage can't see. Every line ran. The edge never got checked.
Running it on a Java service with PIT
For Java, the standard tool is PIT (pitest). It works with Maven and Gradle, and it's fast because it mutates bytecode and only runs the tests that cover each mutated line.
With Maven and JUnit 5:
<plugin>
<groupId>org.pitest</groupId>
<artifactId>pitest-maven</artifactId>
<version>${pitest.version}</version>
<dependencies>
<dependency>
<groupId>org.pitest</groupId>
<artifactId>pitest-junit5-plugin</artifactId>
<version>${pitest-junit5.version}</version>
</dependency>
</dependencies>
<configuration>
<targetClasses>
<param>com.example.orders.*</param>
</targetClasses>
<mutationThreshold>70</mutationThreshold>
<withHistory>true</withHistory>
</configuration>
</plugin>Then:
mvn test-compile org.pitest:pitest-maven:mutationCoverageYou get an HTML report showing each line, the mutants made there, and which survived. The surviving ones are your to-do list.
A few settings worth knowing:
targetClasseskeeps it focused. Mutating DTOs and generated code is a waste of CPU and patience.mutationThresholdfails the build below a score, the same way a JaCoCo coverage gate does.withHistorystores results between runs so only changed code gets re-mutated. On a big service this turns a coffee-break run into a quick one.
Why this matters more with AI-written tests
AI test generation has a specific failure pattern. The tests are structurally fine and semantically thin. They mock the right collaborators, call the right method, and assert something that's true but doesn't pin the behaviour down.
Typical survivors in AI-generated suites:
- Weak assertions.
assertNotNull(result)orassertTrue(list.size() > 0)when the real requirement is a specific value. - Verify-only tests.
verify(repository).save(any())confirms a save happened, not what was saved. - Happy-path only. Generated tests cover the obvious case and skip the boundaries, which are exactly where mutants hide.
- Tests that mirror the implementation. The test computes the expected value with the same formula as the code, so a bug in the formula shows up on both sides and cancels out. (Two wrongs making a green build.)
Mutation testing catches all four, mechanically, without anyone having to read five hundred tests line by line.
A workflow that works
This loop keeps AI-generated tests honest without slowing you down much:
- Generate tests with your AI tool of choice, the normal way.
- Run coverage with your usual gate. This catches untested code.
- Run PIT on the classes you just tested. This catches tests that don't check anything.
- Feed the survivors back. Give the tool the surviving mutants ("this test suite does not fail when
>=on line 42 becomes>") and ask for tests that kill them. AI tools are genuinely good at this. A surviving mutant is a precise, small task, which is the kind they handle best. - Review the new tests like any other code.
Step 4 is the nice surprise. "Write tests for this class" is a vague request and gets vague tests. "Write a test that fails when this specific line changes" is a sharp request and gets a sharp test.
What score should you aim for?
Not 100%. Some mutants are equivalent: the mutation doesn't change behaviour, so no test could ever kill it. Changing i < list.size() to i != list.size() in a simple loop is a classic. Chasing those is a fine way to spend a week achieving nothing.
Reasonable targets:
- Core business logic (pricing, eligibility, state transitions, money): 70–85%.
- Glue code (controllers, mappers, config): don't bother mutating it; exclude it.
- New code: hold it to the bar, even if old code is lower. Use
withHistoryand run it on changed classes in CI.
Pair it with your coverage gate rather than replacing it. Coverage answers "did we test it at all?". Mutation score answers "would the tests notice if it broke?". You want both answers.
The costs, honestly
Mutation testing is slower than running tests once, because it runs them many times. On a large module a full run can take a while. That's why most teams:
- run it nightly or on a schedule for the full codebase, and
- run it incrementally in pull requests, only on changed classes.
It also produces a report someone has to read. The first run on an old codebase usually returns a long list of survivors, which can be a bit humbling. Don't fix them all. Fix the ones in code where a bug costs money, and set the threshold for new code.
The takeaway
Coverage tells you the code ran. It has never told you that a test would fail if the code broke, and AI-generated suites make that gap wide enough to ship through. The tests mock the right collaborators and call the right methods, then assert assertNotNull, verify that a save happened without checking what was saved, skip the boundaries, or compute the expected value with the same formula as the code. Mutation testing plants one small bug at a time and checks that something complains. Every survivor is a bug your suite would have waved through, and you find it without anyone reading five hundred tests line by line.
Run PIT on the classes where a bug costs money, and treat the survivors as prompts rather than chores. "Write a test that fails when this line changes" is exactly the kind of narrow task AI tools handle well, so the same tool that wrote the thin tests can thicken them. Aim for 70 to 85% on business logic, exclude glue code, gate new code incrementally in pull requests, and leave the full run to a nightly job. Keep the coverage gate as well, because the two answer different questions.
The fastest way to write a lot of tests is to have a machine do it. The fastest way to find out which of those tests are decorative is to have another machine try to break your code.
/share

Kishore K Sharma
Module Lead | Senior Full Stack Engineer | Java · Spring Boot · Distributed Systems · AWS | Building Scalable Cloud-Native Platforms
Available for contract work and remote full-time roles. See what I take on.