Scrivio Interview library
ConceptIntermediate7 min read · updated 12 November 2026

What is mutation testing, how does PIT work in a Java build, and what does a surviving mutant tell you?

Mutation testing makes many small, deliberate changes to our code, called mutants, and runs the tests against each one to see whether any test fails. PIT (pitest) does this for Java by changing compiled bytecode in memory and running only the tests that cover the changed line, through a Maven or Gradle plugin. A mutant that no test notices, a surviving mutant, tells us that some behaviour is executed by the tests but never actually checked.

Why this question is asked

Coverage tells us which lines our tests run, but not whether the tests would notice if those lines were wrong. Teams discover this the hard way when a suite with 85 percent coverage lets an obvious bug through. Interviewers ask about mutation testing to see whether we understand the difference between running code and testing it, and whether we know how to use an expensive tool sensibly. In this answer, we will recap the idea, look at how PIT works inside a Java build, read a surviving mutant in a real example, and finish with how to keep the cost under control.

1A Quick Recap of the Idea

The question mutation testing asks is simple: if somebody introduced a small bug here, would our tests catch it? Instead of waiting for a real bug, the tool creates fake ones on purpose. Each fake bug is a mutant: a copy of the program with one small change, such as > turned into >=, + turned into -, a return value replaced with null, or a method call removed.

For every mutant, the tool runs the tests. If at least one test fails, the mutant is killed, which is the good outcome: our tests noticed the change. If every test still passes, the mutant survived, which means our tests could not tell the broken program from the correct one. The mutation score is the share of mutants killed, for example 180 killed out of 200 gives 90 percent.

Notice the difference from line coverage. Coverage asks whether a test executed the line. Mutation testing asks whether a test would fail if the line were wrong. A test with no assertions can give 100 percent coverage and kill zero mutants.

2How PIT Works Inside a Java Build

PIT is the standard mutation testing tool for the JVM. It does not edit our source files and recompile them, which would be far too slow. It works on the compiled bytecode instead. The steps are worth knowing because each one exists to save time.

First, PIT runs the whole test suite once without any mutation and records which tests cover which lines. Then it generates mutants by applying its mutation operators, called mutators, to the bytecode of the classes we target. For each mutant it starts or reuses a separate worker JVM, loads the mutated class into it, and runs only the tests that cover the mutated line, stopping at the first failure. A test that runs much longer than it did in the clean run is treated as a timeout, which usually means the mutant created an infinite loop, and that counts as killed.

Step throughWhat PIT does for one class
  1. Run the clean test suiterecord which tests cover which lines
  2. Create mutants in bytecodefor example > becomes >=, a return becomes null
  3. Load one mutant in a worker JVM
  4. Run only the covering testsstop at the first failure
  5. Did any test fail or time out?
    YesKilled: the tests noticedNoSurvived: a gap in the assertions
  6. Write the HTML and XML reportper line, with each mutant's status

6 steps in order. Play them, or step through them one at a time.

PIT only runs the tests that touch a mutated line, which keeps the cost manageable.

So the number of test runs is roughly the number of mutants times the few tests that cover each one, not mutants times the whole suite. That targeting is what makes PIT usable at all on real projects.

3Running PIT With Maven or Gradle

In Maven we add the pitest-maven plugin and, for JUnit 5 or 6 tests, the pitest-junit5-plugin as a dependency of the plugin, because PIT needs an adapter for the test engine. In Gradle the usual plugin is info.solidsoft.pitest, configured the same way.

XML
<plugin>
<groupId>org.pitest</groupId>
<artifactId>pitest-maven</artifactId>
<version><!-- current release --></version>
<dependencies>
<dependency>
<groupId>org.pitest</groupId>
<artifactId>pitest-junit5-plugin</artifactId>
<version><!-- current release --></version>
</dependency>
</dependencies>
<configuration>
<targetClasses>com.shop.pricing.*</targetClasses>
<targetTests>com.shop.pricing.*</targetTests>
<threads>4</threads>
</configuration>
</plugin>
// mvn test-compile org.pitest:pitest-maven:mutationCoverage
// >> Generated 214 mutations Killed 181 (85%)
// report written to target/pit-reports/index.html

targetClasses limits which code is mutated, and threads lets PIT use several workers in parallel. The report shows each line of the source in green or red, and lists every mutant with its status: KILLED, SURVIVED, NO_COVERAGE (no test even runs the line), or TIMED_OUT. Exact plugin versions change often, so check the current release when you set it up.

4Reading a Surviving Mutant

Here is a small example. A shipping rule gives free delivery for orders of 50 or more, and a test that looks reasonable.

Java
class Shipping {
BigDecimal cost(BigDecimal total) {
if (total.compareTo(new BigDecimal("50")) >= 0) return BigDecimal.ZERO;
return new BigDecimal("4.99");
}
}
@Test void freeShippingForLargeOrders() {
assertEquals(BigDecimal.ZERO, shipping.cost(new BigDecimal("80"))); // passes
}
@Test void paidShippingForSmallOrders() {
assertEquals(new BigDecimal("4.99"), shipping.cost(new BigDecimal("20"))); // passes
}
// PIT: changed conditional boundary (>= became >) -> SURVIVED

Coverage for this method is 100 percent, lines and branches. Yet PIT changed >= to > and both tests still passed, because neither test uses a total of exactly 50. The surviving mutant is pointing at a precise gap: nothing checks what happens at the boundary, which is exactly where real off-by-one bugs live. The fix is one more test with a total of 50 that expects zero. With it, the mutant is killed.

So a surviving mutant is not a bug in the code. It is a question addressed to us: is this behaviour important? If yes, a test is missing or an assertion is too weak. If no, perhaps the code itself is unnecessary. Occasionally the answer is that the mutant is equivalent, meaning the change does not alter behaviour at all, for example changing a loop condition that can never reach the boundary. Such mutants cannot be killed and should simply be ignored.

5Keeping the Cost Under Control

Mutation testing is much slower than coverage. A module with 3,000 mutants and covering tests that take 200 milliseconds each needs on the order of ten minutes even with targeting, and a large service can take hours. That is why teams rarely run it on every class on every build.

The usual approach is to run it where it pays. We run it on critical modules, such as pricing, payments or authorisation rules, where a missed defect is expensive. We run it on the code a pull request changed, using PIT's history files for incremental analysis or the scmMutationCoverage goal, which limits mutation to files changed in version control (check your plugin version for the exact options). And we run a full analysis nightly rather than on every commit. Used this way, mutation testing becomes a focused review tool rather than a slow gate.

6Follow-up questions

Why not simply rely on branch coverage?

Because branch coverage only proves that a path was taken, not that the result of that path was checked. In the shipping example, both branches were covered and still the boundary was untested. Mutation testing measures whether the assertions are strong enough to notice a wrong result, which is what we actually care about.

What is an equivalent mutant?

It is a mutant that changes the code without changing its observable behaviour, so no test can ever kill it. For example, mutating a condition in a cache check might only make the code slower, not wrong. Equivalent mutants are the reason a 100 percent mutation score is not a sensible goal; a score in the high 80s on a critical module is usually excellent.

Does PIT work with Spring Boot tests?

It does, but slow tests make mutation testing very slow, because each mutant reruns them. In practice we target plain unit tests of domain classes and exclude tests that start a Spring context or containers, using targetTests or excluded test classes in the plugin configuration.

7Conclusion

In this answer, we saw that mutation testing creates small deliberate bugs and checks whether the tests catch them. PIT does this on bytecode, runs only the tests that cover each mutated line, and reports each mutant as killed or survived. A surviving mutant points to behaviour that is executed but not checked, often a boundary or a missing assertion. Because it is slow, we run it on critical modules, on changed code and nightly, rather than on everything for every build.

What to say in the interviewAbout 90 seconds aloud

Open with

Mutation testing checks the quality of the tests rather than the code. The tool makes small changes to the program, like turning greater-or-equal into greater, negating a condition or returning null, and each changed version is a mutant.

Then, if they want more

It runs the tests against each mutant. If a test fails, the mutant is killed, which is good. If all tests still pass, the mutant survived, which means the tests execute that code but never check its result. In Java the tool is PIT.

It runs the suite once to learn which tests cover which lines, then mutates the bytecode in memory, loads each mutant into a worker JVM and runs only the covering tests. It has a Maven plugin, with the JUnit 5 plugin as a dependency, and a Gradle plugin, and it produces an HTML report per line.

A typical surviving mutant I have seen is a boundary: a free-shipping rule at 50 that was fully covered, but no test used exactly 50, so changing the comparison went unnoticed. Some mutants are equivalent and can never be killed, so I never aim for 100 percent.

It is much slower than coverage, so I run it on critical modules like pricing, on the files changed in a pull request, and as a nightly job, not on every build.

About this answer

Last updated
12 November 2026
Versions it names
JUnit 5
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What is mutation testing, how does PIT work in a Java build, and what does a surviving...": https://scrivio-interview-library.netlify.app/java/test-strategy/5a4d570bab/