Skip to content

Mutation Tests & Failure Evidence

The Course
•
20 min study

A test suite can pass while missing the behavior it claims to protect. Mutation testing deliberately introduces a plausible defect and checks that the suite fails for the right reason. A compile error is not evidence that a behavioral assertion detected the defect.

Understand the contract

Choose one small mutation at a time: change an inclusive comparison to a strict one, omit a tie-breaker, remove a defensive copy, or commit a candidate too early. Run the same checks. If they still pass, either the mutation did not change observable behavior for those inputs or your tests need a better case.

The first example compares the intended predicate with a deliberately wrong predicate on a fixed boundary. The second runs a whole suite against both and lets you watch the mutant survive. In a real project you temporarily edit the implementation and run its existing tests, then restore it; here the mutant sits beside the original so one run shows both.

An assertion name should identify a promise, not just say “failed.” A mutation that throws an unrelated NullPointerException may stop the suite, but does not necessarily show the intended check is strong. Inspect the actual failing case and exception. The final project's automated verification uses specific mutations and requires AssertionError from its behavioral tests.

Before running, write the output you expect and explain which rule produces each line. Each editor is an independent program.

Predict, then run Java
public class Main {
    public static void main(String[] args) {
        int boundary = 100;
        System.out.println(allowed(boundary));
        System.out.println(broken(boundary));
        System.out.println(allowed(boundary) != broken(boundary));
    }

    static boolean allowed(int value) {
        return value >= 1 && value <= 100;
    }

    static boolean broken(int value) {
        return value >= 1 && value < 100;
    }
}
Check your prediction
Text
true
false
true

Watch a mutant survive

The playground above compared two predicates directly. A real suite is less lucky: it has a fixed set of cases, written before anyone imagined this mutant. Below, the same three-case suite runs against the real rule and against the <= 100 to < 100 mutant. Predict both scores.

A suite that cannot see the mutant Java
import java.util.function.IntPredicate;

public class Main {
    public static void main(String[] args) {
        System.out.println("Real: " + passed(Main::allowed) + "/3");
        System.out.println("Mutant: " + passed(Main::mutant) + "/3");
    }

    static int passed(IntPredicate rule) {
        int passed = 0;
        if (rule.test(50)) {
            passed++;
        }
        if (!rule.test(0)) {
            passed++;
        }
        if (!rule.test(150)) {
            passed++;
        }
        return passed;
    }

    static boolean allowed(int value) {
        return value >= 1 && value <= 100;
    }

    static boolean mutant(int value) {
        return value >= 1 && value < 100;
    }
}
Check your prediction
Text
Real: 3/3
Mutant: 3/3

The suite passes against both, so it cannot tell them apart: the mutant survives. Every case is either well inside the range or well outside it. The two rules disagree only at 100, and no case asks about 100. Add if (rule.test(100)) { passed++; } and change both /3 labels to /4: the real rule scores 4/4 and the mutant 3/4. That one case is the whole difference between a suite that looks thorough and one that protects the boundary.

Require a behavioral reason for the failure

A meaningful mutation changes one plausible decision while keeping the program compilable. Then the existing checks should fail at a named assertion. Removing a brace produces a compiler error, but tells you nothing about the strength of a rollback or ownership test.

Read the failed assertion after every experiment. A mutation that causes an unrelated early exception might prevent the intended observation from running. You want evidence that the disputed promise was checked, not merely that execution stopped somewhere.

Restore a passing baseline before the next mutation. Keep a small list linking each defect to a distinguishing input: late conflict for early commit, equal primary keys for a missing tie-breaker, and source/copy edits for aliasing. This turns test maintenance into an argument about observable behavior.

Build it yourself

Exercise

Kill the surviving mutant

ranked orders scores from highest to lowest, and breaks ties by name, A to Z. rankedMutant is the same method with the tie-breaker deleted. The suite runs against both: the real version must pass and the mutant should fail, but right now it survives. Add one check to runSuite so the program prints Real passed and then Mutant killed: ties ordered by name. Do not change either ranking method.

Your workspace Java
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.function.Function;

public class Main {
    public static void main(String[] args) {
        runSuite(Main::ranked);
        System.out.println("Real passed");
        try {
            runSuite(Main::rankedMutant);
            System.out.println("Mutant survived");
        } catch (AssertionError killed) {
            System.out.println("Mutant killed: " + killed.getMessage());
        }
    }

    static void runSuite(Function<List<Score>, List<Score>> rank) {
        List<Score> two = rank.apply(List.of(new Score("Bo", 3), new Score("Al", 9)));
        check("highest first", two.get(0).name().equals("Al"));
        check("empty", rank.apply(List.of()).isEmpty());
        // Add one test here that the mutant fails.
    }

    static List<Score> ranked(List<Score> scores) {
        Comparator<Score> byPoints = Comparator.comparingInt(Score::points);
        List<Score> copy = new ArrayList<>(scores);
        copy.sort(byPoints.reversed().thenComparing(Score::name));
        return copy;
    }

    // Mutant: the tie-breaker was deleted.
    static List<Score> rankedMutant(List<Score> scores) {
        Comparator<Score> byPoints = Comparator.comparingInt(Score::points);
        List<Score> copy = new ArrayList<>(scores);
        copy.sort(byPoints.reversed());
        return copy;
    }

    static void check(String name, boolean condition) {
        if (!condition) {
            throw new AssertionError(name);
        }
    }
}

record Score(String name, int points) {}
Hint 1: the idea

Ask where the two methods can disagree. They differ only when two scores have the same points. Neither existing case has a tie.

Hint 2: a first step

Rank two scores with equal points and check which name comes first. Be careful about the input order: List.sort is stable, so if the input is already in name order, the mutant leaves it that way and still passes.

Show one solution
Java
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.function.Function;

public class Main {
    public static void main(String[] args) {
        runSuite(Main::ranked);
        System.out.println("Real passed");
        try {
            runSuite(Main::rankedMutant);
            System.out.println("Mutant survived");
        } catch (AssertionError killed) {
            System.out.println("Mutant killed: " + killed.getMessage());
        }
    }

    static void runSuite(Function<List<Score>, List<Score>> rank) {
        List<Score> two = rank.apply(List.of(new Score("Bo", 3), new Score("Al", 9)));
        check("highest first", two.get(0).name().equals("Al"));
        check("empty", rank.apply(List.of()).isEmpty());
        List<Score> tied = rank.apply(List.of(new Score("Cy", 5), new Score("Al", 5)));
        check("ties ordered by name", tied.get(0).name().equals("Al"));
    }

    static List<Score> ranked(List<Score> scores) {
        Comparator<Score> byPoints = Comparator.comparingInt(Score::points);
        List<Score> copy = new ArrayList<>(scores);
        copy.sort(byPoints.reversed().thenComparing(Score::name));
        return copy;
    }

    // Mutant: the tie-breaker was deleted.
    static List<Score> rankedMutant(List<Score> scores) {
        Comparator<Score> byPoints = Comparator.comparingInt(Score::points);
        List<Score> copy = new ArrayList<>(scores);
        copy.sort(byPoints.reversed());
        return copy;
    }

    static void check(String name, boolean condition) {
        if (!condition) {
            throw new AssertionError(name);
        }
    }
}

record Score(String name, int points) {}

Expected output:

Text
Real passed
Mutant killed: ties ordered by name

The new case has a tie, and its input puts Cy before Al. The real ranking uses the name to put Al first; the mutant has no tie-breaker, so the stable sort keeps Cy first and the named check fails. With the input Al, Cy the mutant would have survived again, which is why a killing test has to be designed against the mutant, not just added. The mutant fails with an AssertionError naming the promise, not with an unrelated exception.

Change it and test again

Create a mutation checklist for the batch checkpoint: alias the original map, skip within-batch conflict checks, or treat a conflicting ID as a replay. Give each defect a concrete test input. Run one defect at a time and restore a passing baseline between experiments so failures remain attributable.

Explain before moving on

Why is a high test count weaker evidence than a few tests that distinguish plausible defects? Explain why mutation testing supplements thoughtful contracts rather than proving the implementation correct for every possible input.

Was this article helpful?

Your response is saved on this device.