Skip to content
mutastic

Coverage and mutants

A test can run every line of your code, and both sides of every if, without checking what any of it does. Coverage scores it the same as a test that checks everything, so the report is green while the tests would pass on code that’s wrong. mutastic asks the question coverage can’t: it puts a small bug in the code, runs the tests, and tells you when they don’t notice.

This page measures one small example both ways, with tests that check too little and tests that check the results. Coverage can’t tell them apart, and mutastic can. Then it shows where mutants reach past branch coverage even for good tests, and when --operators=stronger finds more than the defaults.

  • Line coverage asks which lines ran. A line no test runs is untested, for sure. A line that ran may be untested too: running it isn’t checking it.
  • Branch coverage asks which way each decision went: whether the tests took each if both ways. It finds the side of a condition no test takes. It still doesn’t look at what the tests assert.
  • Mutation testing asks whether the tests would notice a bug. mutastic makes one small change to the code at a time, a mutant, and runs the tests that reach it. A mutant they catch is code they check. One they miss is a gap, on a line coverage counts as tested.

Coverage is cheap, and it tells you where tests never go. Only mutants tell you whether the tests check what they reach.

The example works out what an order costs:

public class Checkout {
/// 10% off orders of 100 or more.
public int discount(int total) {
if (total >= 100) {
return total / 10;
}
return 0;
}
/// What `quantity` items at `price` each cost, less any discount, which goes on the receipt.
public int pay(int quantity, int price, List<String> receipt) {
int total = quantity * price;
int off = discount(total);
receipt.add("discount " + off);
return total - off;
}
}

Its tests run every line, and take the if both ways: a big order gets the discount and a small one doesn’t. But one checks only that a big order costs less than 250, and the other checks nothing at all:

class CheckoutTest {
@Test
void bigOrdersGetADiscount() {
var receipt = new ArrayList<String>();
assertTrue(new Checkout().pay(1, 250, receipt) < 250);
}
@Test
void smallOrdersPayFullPrice() {
new Checkout().pay(1, 50, new ArrayList<>());
}
}

These check the results: what each order costs, and what goes on the receipt.

With import static org.junit.jupiter.api.Assertions.assertEquals; and import java.util.List; at the top of the file:

@Test
void bigOrdersGetADiscount() {
var receipt = new ArrayList<String>();
assertEquals(225, new Checkout().pay(1, 250, receipt));
assertEquals(List.of("discount 25"), receipt);
}
@Test
void smallOrdersPayFullPrice() {
var receipt = new ArrayList<String>();
assertEquals(50, new Checkout().pay(1, 50, receipt));
assertEquals(List.of("discount 0"), receipt);
}

JaCoCo measures their coverage. The sample’s build applies its Gradle plugin, and its CSV report has the counts:

Terminal window
./gradlew test jacocoTestReport
TestsLinesBranches
Checking too little8 of 82 of 2
Checking the results8 of 82 of 2

Every line ran, and the if went both ways, with either set of tests.

The tests that check too little:

Terminal window
mutastic run samples/checkout --format=concise
Terminal window
Missed ────────────────────────────────────────────────────────────
✗ src/main/java/com/example/checkout/Checkout.java:8 no test checks discount() at total == 100 mc5ea6505
✗ src/main/java/com/example/checkout/Checkout.java:8 no test needs total >= 100 to be false in discount() m56bd9867
✗ src/main/java/com/example/checkout/Checkout.java:9 tests pass even when / becomes * in discount() mee8d2a4d
✗ src/main/java/com/example/checkout/Checkout.java:11 no test checks discount() returning 0 here mf4e4c49c
✗ src/main/java/com/example/checkout/Checkout.java:16 tests pass even when * becomes / in pay() m21faa923
✗ src/main/java/com/example/checkout/Checkout.java:18 no test checks the effect of calling add() in pay() md47a74cb
✗ src/main/java/com/example/checkout/Checkout.java:19 no test checks what pay() returns here m6073d12b
Next: mutastic verify <id> samples/checkout · mutastic run --only=<id> samples/checkout · mutastic explain <id> samples/checkout
Classes 0 clean | 1 with misses
Mutants ✓ 3 caught · ✗ 7 missed (10)
Worst at Checkout.discount() 4 ✗ · Checkout.pay() 3 ✗
Test runs 17 (20 without coverage selection)
Start at 19:09:33
Duration 1.7s (setup 0ms, build 441ms, coverage 190ms, mutants 146ms, checking 949ms, reports 12ms)
Tests 2 on JDK 17 · 1 worker
Reports samples/checkout/build/mutastic/mutation.json · samples/checkout/build/mutastic/mutation.html
Score 30.0% FAIL Below the threshold of 80: 7 missed, 0 not covered.

Each mutant under Missed is a bug those tests let through, on code that coverage counts as tested. With the tests that check the results, mutastic catches all of them but one, and that one is the first of the next section.

The tests that check the results take the if both ways, at 50 and 250. Branch coverage asks for nothing more. But no test is at 100, so >= and > give the same answer on every test, and the off-by-one bug passes:

Terminal window
mutastic run samples/checkout
Terminal window
Missed ────────────────────────────────────────────────────────────
❯ src/main/java/com/example/checkout/Checkout.java 1 missed in 1 method
✗ MISSED no test checks discount() at total == 100
┌─ src/main/java/com/example/checkout/Checkout.java:8
│
7 │ public int discount(int total) {
8 │ if (total >= 100) {
│ ^^ >= changed to >
9 │ return total / 10;
│
= mutant - if (total >= 100) {
+ if (total > 100) {
= operator mutastic/boundary · https://docs.mutastic.io/java/operators/conditions/#boundary
= note: every test calls discount() away from the boundary
inputs seen: 50, 250 (none is 100)
covered by CheckoutTest#bigOrdersGetADiscount, CheckoutTest#smallOrdersPayFullPrice (all 2 covering tests still pass)
= help: test discount() with total = 100 and assert the result
// in CheckoutTest
@Test
void discountAtTheTotalEdge() {
assertEquals(10, new Checkout().discount(100)); // today 10 · with this mutant 0
}
check today's value is the one you intend: the test pins what the code does now
done when mutastic verify mc5ea6505 samples/checkout passes
reproduce mutastic run --only=mc5ea6505 samples/checkout
or ignore mutastic ignore mc5ea6505 --reason="<why>" samples/checkout
Next: mutastic verify <id> samples/checkout · mutastic run --only=<id> samples/checkout · mutastic explain <id> samples/checkout
Classes 0 clean | 1 with misses
Mutants ✓ 9 caught · ✗ 1 missed (10)
Worst at Checkout.discount() 1 ✗
Test runs 13 (20 without coverage selection)
Start at 03:35:54
Duration 1.8s (setup 0ms, build 458ms, coverage 198ms, mutants 113ms, checking 1.1s, reports 10ms)
Tests 2 on JDK 17 · 1 worker
Reports samples/checkout/build/mutastic/mutation.json · samples/checkout/build/mutastic/mutation.html
Score 90.0% PASS 1 missed, above the threshold of 80.

The card asks for a test at 100. Here’s that one, through pay() as the others go, and one just below the edge, so the edge is pinned from both sides:

@Test
void ordersOf100GetADiscount() {
assertEquals(90, new Checkout().pay(1, 100, new ArrayList<>()));
}
@Test
void ordersJustUnder100PayFullPrice() {
assertEquals(99, new Checkout().pay(1, 99, new ArrayList<>()));
}

Now the run catches every mutant:

Terminal window
mutastic run samples/checkout
Terminal window
Classes 1 clean
Mutants ✓ 10 caught (10)
Test runs 18 (40 without coverage selection)
Start at 19:09:41
Duration 2.4s (setup 0ms, build 1.1s, coverage 193ms, mutants 87ms, checking 951ms, reports 10ms)
Tests 4 on JDK 17 · 1 worker
Reports samples/checkout/build/mutastic/mutation.json · samples/checkout/build/mutastic/mutation.html
Score 100.0% PASS Every mutant was caught.

Every test buys one item, and 1 / 250 is 0. With * changed to /, pay() charges nothing, and nothing is less than 250, so the test that checks too little still passes. Coverage counted the line; nothing checked what it worked out.

Terminal window
✗ MISSED tests pass even when * becomes / in pay()
┌─ src/main/java/com/example/checkout/Checkout.java:16
│
15 │ public int pay(int quantity, int price, List<String> receipt) {
16 │ int total = quantity * price;
│ ^ * changed to /
17 │ int off = discount(total);
│
= mutant - int total = quantity * price;
+ int total = quantity / price;
= operator mutastic/math · https://docs.mutastic.io/java/operators/arithmetic/#math
= note: inputs seen: quantity = 1; price = 50, 250
covered by CheckoutTest#bigOrdersGetADiscount, CheckoutTest#smallOrdersPayFullPrice (all 2 covering tests still pass)
= help: use inputs where quantity * price and quantity / price differ, and assert the result
done when mutastic verify m21faa923 samples/checkout passes
reproduce mutastic run --only=m21faa923 samples/checkout
or ignore mutastic ignore m21faa923 --reason="<why>" samples/checkout

A test that checks only which way a value went passes for a wrong value too. pay() returning 0 is less than 250:

Terminal window
✗ MISSED no test checks what pay() returns here
┌─ src/main/java/com/example/checkout/Checkout.java:19
│
18 │ receipt.add("discount " + off);
19 │ return total - off;
│ ^^^^^^^^^^^^^^^^^^^ now returns 0
20 │ }
│
= mutant - return total - off;
+ return 0;
= operator mutastic/return-value · https://docs.mutastic.io/java/operators/returns/#return-value
= note: tests reach this return, but none fails when it returns 0
inputs seen: quantity = 1; price = 50, 250
covered by CheckoutTest#bigOrdersGetADiscount, CheckoutTest#smallOrdersPayFullPrice (all 2 covering tests still pass)
= help: assert the value pay() returns on this path, with inputs where it isn't 0
done when mutastic verify m6073d12b samples/checkout passes
reproduce mutastic run --only=m6073d12b samples/checkout
or ignore mutastic ignore m6073d12b --reason="<why>" samples/checkout

The receipt is what the customer sees, and neither test looks at it. With the line that writes it gone, both pass:

Terminal window
✗ MISSED no test checks the effect of calling add() in pay()
┌─ src/main/java/com/example/checkout/Checkout.java:18
│
17 │ int off = discount(total);
18 │ receipt.add("discount " + off);
│ ^^^ call removed
19 │ return total - off;
│
= mutant - receipt.add("discount " + off);
= operator mutastic/remove-discarded-call · https://docs.mutastic.io/java/operators/calls/#remove-discarded-call
= note: inputs seen: quantity = 1; price = 50, 250
covered by CheckoutTest#bigOrdersGetADiscount, CheckoutTest#smallOrdersPayFullPrice (all 2 covering tests still pass)
= help: assert what add() does: the state it changes or the call it makes
done when mutastic verify md47a74cb samples/checkout passes
reproduce mutastic run --only=md47a74cb samples/checkout
or ignore mutastic ignore md47a74cb --reason="<why>" samples/checkout

The tests that check the results catch all three: they assert what pay() returns and what’s on the receipt.

A run uses the defaults set of operators: the changes most likely to find a gap worth a test. --operators=stronger adds more (see All operators). Before you pay for a run, mutastic mutants lists what it would make, and runs no test:

Terminal window
mutastic mutants samples/checkout --operators=stronger
Terminal window
m1d8dbce7 src/main/java/com/example/checkout/Checkout.java:8:5 mutastic/constant-value the constant 100 becomes 101 in Checkout.discount()
m7dc17ea7 src/main/java/com/example/checkout/Checkout.java:8:5 mutastic/constant-value the constant 100 becomes 99 in Checkout.discount()
mab1e3cd8 src/main/java/com/example/checkout/Checkout.java:8:5 mutastic/constant-value the constant 100 becomes 0 in Checkout.discount()
m0d5e83ae src/main/java/com/example/checkout/Checkout.java:8:5 mutastic/constant-value the constant 100 becomes 1 in Checkout.discount()
md6228c60 src/main/java/com/example/checkout/Checkout.java:8:5 mutastic/constant-value the constant 100 becomes -1 in Checkout.discount()
mc5ea6505 src/main/java/com/example/checkout/Checkout.java:8:15 mutastic/boundary >= becomes > in Checkout.discount()
mff367b6e src/main/java/com/example/checkout/Checkout.java:8:15 mutastic/comparison >= becomes == in Checkout.discount()
m28f01dd9 src/main/java/com/example/checkout/Checkout.java:8:15 mutastic/other-comparison >= becomes < in Checkout.discount()
m8285ace9 src/main/java/com/example/checkout/Checkout.java:8:15 mutastic/other-comparison >= becomes <= in Checkout.discount()
m85a6ca44 src/main/java/com/example/checkout/Checkout.java:8:15 mutastic/other-comparison >= becomes != in Checkout.discount()
m310b6020 src/main/java/com/example/checkout/Checkout.java:8:15 mutastic/remove-conditional total >= 100 is always false in Checkout.discount()
m56bd9867 src/main/java/com/example/checkout/Checkout.java:8:15 mutastic/remove-conditional total >= 100 is always true in Checkout.discount()
m3616c2a8 src/main/java/com/example/checkout/Checkout.java:9:7 mutastic/constant-value the constant 10 becomes 11 in Checkout.discount()
meee1003c src/main/java/com/example/checkout/Checkout.java:9:7 mutastic/constant-value the constant 10 becomes 9 in Checkout.discount()
m36cc35ce src/main/java/com/example/checkout/Checkout.java:9:7 mutastic/constant-value the constant 10 becomes 0 in Checkout.discount()
m04c45bb5 src/main/java/com/example/checkout/Checkout.java:9:7 mutastic/constant-value the constant 10 becomes 1 in Checkout.discount()
m7afdd99a src/main/java/com/example/checkout/Checkout.java:9:7 mutastic/constant-value the constant 10 becomes -1 in Checkout.discount()
m8cecf405 src/main/java/com/example/checkout/Checkout.java:9:7 mutastic/keep-operand total / 10 becomes total in Checkout.discount()
m2413972b src/main/java/com/example/checkout/Checkout.java:9:7 mutastic/return-value returns 0 instead in Checkout.discount()
mee8d2a4d src/main/java/com/example/checkout/Checkout.java:9:20 mutastic/math / becomes * in Checkout.discount()
mf8a628eb src/main/java/com/example/checkout/Checkout.java:9:20 mutastic/other-arithmetic / becomes + in Checkout.discount()
m95f86040 src/main/java/com/example/checkout/Checkout.java:9:20 mutastic/other-arithmetic / becomes - in Checkout.discount()
m51b57889 src/main/java/com/example/checkout/Checkout.java:9:20 mutastic/other-arithmetic / becomes % in Checkout.discount()
mb6127f18 src/main/java/com/example/checkout/Checkout.java:11:5 mutastic/constant-value the constant 0 becomes 1 in Checkout.discount()
m6410385b src/main/java/com/example/checkout/Checkout.java:11:5 mutastic/constant-value the constant 0 becomes -1 in Checkout.discount()
mf4e4c49c src/main/java/com/example/checkout/Checkout.java:11:5 mutastic/return-value returns 1 instead in Checkout.discount()
mc86060dc src/main/java/com/example/checkout/Checkout.java:16:5 mutastic/null-parameter receipt is null from the start of the method in Checkout.pay()
me11e42ae src/main/java/com/example/checkout/Checkout.java:16:5 mutastic/keep-operand quantity * price becomes quantity in Checkout.pay()
mf248f66b src/main/java/com/example/checkout/Checkout.java:16:5 mutastic/keep-operand quantity * price becomes price in Checkout.pay()
m21faa923 src/main/java/com/example/checkout/Checkout.java:16:26 mutastic/math * becomes / in Checkout.pay()
m55a75e34 src/main/java/com/example/checkout/Checkout.java:16:26 mutastic/other-arithmetic * becomes + in Checkout.pay()
m3ab63032 src/main/java/com/example/checkout/Checkout.java:16:26 mutastic/other-arithmetic * becomes - in Checkout.pay()
m68b8adfd src/main/java/com/example/checkout/Checkout.java:16:26 mutastic/other-arithmetic * becomes % in Checkout.pay()
m23048dc9 src/main/java/com/example/checkout/Checkout.java:17:15 mutastic/omit-call discount() is left out, so what it's given goes on in its place in Checkout.pay()
md47a74cb src/main/java/com/example/checkout/Checkout.java:18:13 mutastic/remove-discarded-call the call to add() is removed in Checkout.pay()
mee1fc9ef src/main/java/com/example/checkout/Checkout.java:19:5 mutastic/keep-operand total - off becomes total in Checkout.pay()
m45d77da9 src/main/java/com/example/checkout/Checkout.java:19:5 mutastic/keep-operand total - off becomes off in Checkout.pay()
m6073d12b src/main/java/com/example/checkout/Checkout.java:19:5 mutastic/return-value returns 0 instead in Checkout.pay()
m03964b29 src/main/java/com/example/checkout/Checkout.java:19:18 mutastic/math - becomes + in Checkout.pay()
m5ba95ca2 src/main/java/com/example/checkout/Checkout.java:19:18 mutastic/other-arithmetic - becomes * in Checkout.pay()
mf7012a37 src/main/java/com/example/checkout/Checkout.java:19:18 mutastic/other-arithmetic - becomes / in Checkout.pay()
md46482dd src/main/java/com/example/checkout/Checkout.java:19:18 mutastic/other-arithmetic - becomes % in Checkout.pay()
estimate: 32 more mutants than defaults, up to 1.0s more of tests, from the run of 28 Nov 2026 23:52 UTC
42 mutants

The line before the count is an estimate, from what the last run’s coverage pass measured: what the set’s extra mutants would add to the tests’ time, at most, as a caught mutant stops at its first failing test. Where that run measured nothing, or the code has changed since, it says so instead of guessing; mutastic mutants --estimate runs every test once, with no mutant in, and estimates defaults, stronger and all.

With every test so far in, the defaults catch everything. stronger finds one more:

Terminal window
mutastic run samples/checkout --operators=stronger
Terminal window
Missed ────────────────────────────────────────────────────────────
❯ src/main/java/com/example/checkout/Checkout.java 1 missed in 1 method
✗ MISSED no test needs quantity in quantity * price in pay()
┌─ src/main/java/com/example/checkout/Checkout.java:16
│
15 │ public int pay(int quantity, int price, List<String> receipt) {
16 │ int total = quantity * price;
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ one value kept
17 │ int off = discount(total);
│
= mutant - int total = quantity * price;
+ int total = price;
= operator mutastic/keep-operand · https://docs.mutastic.io/java/operators/arithmetic/#keep-operand
= note: inputs seen: quantity = 1; price = 50, 99, 100, 250
covered by CheckoutTest#bigOrdersGetADiscount, CheckoutTest#ordersJustUnder100PayFullPrice, CheckoutTest#ordersOf100GetADiscount and 1 more (all 4 covering tests still pass)
= help: add a test where quantity changes the result of quantity * price, and assert it
done when mutastic verify mf248f66b samples/checkout passes
reproduce mutastic run --only=mf248f66b samples/checkout
or ignore mutastic ignore mf248f66b --reason="<why>" samples/checkout
Next: mutastic verify <id> samples/checkout · mutastic run --only=<id> samples/checkout · mutastic explain <id> samples/checkout
Classes 0 clean | 1 with misses
Mutants ✓ 41 caught · ✗ 1 missed (42)
Worst at Checkout.pay() 1 ✗
Test runs 60 (168 without coverage selection)
Start at 03:35:59
Duration 2.1s (setup 0ms, build 527ms, coverage 209ms, mutants 338ms, checking 1.0s, reports 33ms)
Tests 4 on JDK 17 · 1 worker
Reports samples/checkout/build/mutastic/mutation.json · samples/checkout/build/mutastic/mutation.html
Score 97.6% PASS 1 missed, above the threshold of 80. · not compared with the last run, which used other operators (defaults)

Every test buys one item, so quantity * price and price are the same on every test: a bug that drops the quantity would ship. The defaults only try * to /, and the tests catch that one, since 1 / 250 is 0. A test that buys more than one closes the gap:

@Test
void severalItemsCostTheirPriceEach() {
assertEquals(60, new Checkout().pay(3, 20, new ArrayList<>()));
}
Terminal window
mutastic verify mf248f66b samples/checkout
Terminal window
mutastic: mf248f66b is caught by com.example.checkout.CheckoutTest#severalItemsCostTheirPriceEach
  • Coverage finds what no test runs. That’s worth knowing, and cheap to measure. mutastic lists it too, under Not covered.
  • A coverage gate says the tests ran the code. mutastic’s score says they checked it. A score can’t be raised by calling code without checking it, which a coverage number can.
  • The defaults first, stronger when they’re all caught. It finds gaps like the one above, at the cost of more mutants to run.

Writing tests first doesn’t close these gaps by itself: Working test-first shows a test written first passing on code that’s wrong, and mutastic finding it in the cycle.