Skip to content
mutastic

Working test-first: the refactor

A refactor is the step where a green suite tells you least. The code takes a new shape and the tests still pass, but passing says they ran the new code, not that they check it. Most test-first guides name the step and move on. This one does it.

Two more cycles grow the rule from Working test-first until the code says the same thing three times. Then the refactor, with mutastic before it and after it, so you know the tests still pin every behaviour of the new shape, not only that they pass. Working test-first, under Concepts, says why mutastic comes where it does.

The spec, a tier at a time: loyal customers get 5 off orders of 30 or more, 10 off orders of 100 or more, and 20 off orders of 250 or more.

Start where Working test-first ends: loyaltyCredit in the pricing sample, with its three tests, and that cycle committed. Each cycle here ends with a commit too: --since=HEAD runs the mutants on the lines changed since the last one.

Working test-first with mutastic: you are at RedRed: a test from the spec fails. Green: the least code that passes it. Mutate: mutastic runs on the cycle's lines. Triangulate: write each card's test and prove it with verify; one that fails on today's code has found a bug, back to green. Refactor, then commit, which starts the next cycle.Reda test failsGreenleast codeMutaterun --since=HEADTriangulatecard's test, verifyRefactorstructure onlyCommitends the cyclered on today's code: a bug, back to greenthe next cycle starts from the commit
  1. Red
  2. Green
  3. Mutate
  4. Triangulate
  5. Refactor
  6. Commit

A test for the new tier:

// 10 off orders of 100 or more.
@Test
void loyalCustomersGetTenOffFrom100() {
assertEquals(10, new Pricing().loyaltyCredit(150));
}

It fails, as it should: the code gives 5.

Terminal window
mutastic run samples/pricing
Terminal window
mutastic: com.example.pricing.PricingTest#loyalCustomersGetTenOffFrom100 fails without any mutant, so mutastic can't judge one.
It failed: expected: <10> but was: <5>
To run without it until it's fixed, list it under exclude-tests: in mutastic.yaml with a reason, or for this run add --exclude-tests=PricingTest#loyalCustomersGetTenOffFrom100
Its output is in samples/pricing/build/mutastic/worker.log. To run it alone:
samples/pricing/gradlew -p samples/pricing test --tests 'com.example.pricing.PricingTest.loyalCustomersGetTenOffFrom100'
Working test-first with mutastic: you are at GreenRed: a test from the spec fails. Green: the least code that passes it. Mutate: mutastic runs on the cycle's lines. Triangulate: write each card's test and prove it with verify; one that fails on today's code has found a bug, back to green. Refactor, then commit, which starts the next cycle.Reda test failsGreenleast codeMutaterun --since=HEADTriangulatecard's test, verifyRefactorstructure onlyCommitends the cyclered on today's code: a bug, back to greenthe next cycle starts from the commit
  1. Red
  2. Green
  3. Mutate
  4. Triangulate
  5. Refactor
  6. Commit

The least code that passes is a second if, above the first, as the higher tier wins. In place of the method:

/// For loyal customers: 5 off orders of 30 or more, and 10 off from 100.
public int loyaltyCredit(int total) {
if (total >= 100) {
return 10;
}
if (total >= 30) {
return 5;
}
return 0;
}

The whole suite passes.

Working test-first with mutastic: you are at MutateRed: a test from the spec fails. Green: the least code that passes it. Mutate: mutastic runs on the cycle's lines. Triangulate: write each card's test and prove it with verify; one that fails on today's code has found a bug, back to green. Refactor, then commit, which starts the next cycle.Reda test failsGreenleast codeMutaterun --since=HEADTriangulatecard's test, verifyRefactorstructure onlyCommitends the cyclered on today's code: a bug, back to greenthe next cycle starts from the commit
  1. Red
  2. Green
  3. Mutate
  4. Triangulate
  5. Refactor
  6. Commit

mutastic on the cycle’s lines:

Terminal window
mutastic run samples/pricing --since=HEAD
Terminal window
Missed ────────────────────────────────────────────────────────────
❯ src/main/java/com/example/pricing/Pricing.java 1 missed in 1 method
✗ MISSED no test checks loyaltyCredit() at total == 100
┌─ src/main/java/com/example/pricing/Pricing.java:33
│
32 │ public int loyaltyCredit(int total) {
33 │ if (total >= 100) {
│ ^^ >= changed to >
34 │ return 10;
│
= mutant - if (total >= 100) {
+ if (total > 100) {
= operator mutastic/boundary · https://docs.mutastic.io/java/operators/conditions/#boundary
= note: every test calls loyaltyCredit() away from the boundary
inputs seen: 29, 30, 50, 150 (none is 100)
covered by PricingTest#loyalCustomersGetFiveOff, PricingTest#loyalCustomersGetTenOffFrom100, PricingTest#loyaltyCreditAtTheTotalEdge and 1 more (all 4 covering tests still pass)
= help: test loyaltyCredit() with total = 100 and assert the result
// in PricingTest
@Test
void loyaltyCreditAtTheTotalEdge2() {
assertEquals(10, new Pricing().loyaltyCredit(100)); // today 10 · with this mutant 5
}
check today's value is the one you intend: the test pins what the code does now
done when mutastic verify m809c9c9b samples/pricing passes
reproduce mutastic run --only=m809c9c9b samples/pricing
or ignore mutastic ignore m809c9c9b --reason="<why>" samples/pricing
Next: mutastic verify <id> samples/pricing · mutastic run --only=<id> samples/pricing · mutastic explain <id> samples/pricing
Classes 0 clean | 1 with misses
Mutants ✓ 3 caught · ✗ 1 missed (4)
Changed 4 lines in 1 file since HEAD
Worst at Pricing.loyaltyCredit() 1 ✗
Test runs 8 (28 without coverage selection)
Start at 03:35:21
Duration 1.9s (setup 0ms, build 479ms, coverage 239ms, mutants 92ms, checking 1.1s, reports 15ms)
Tests 7 on JDK 17 · 1 worker
Reports samples/pricing/build/mutastic/mutation.json · samples/pricing/build/mutastic/mutation.html
Score 75.0% FAIL Below the threshold of 80: 1 missed, 0 not covered.

The new test is at 150, so nothing checks the edge at 100. The new lines’ other mutants are caught by the tests there are: the one at 50 wants 5, not 10, and the new one wants 10, not 5 or 0.

Working test-first with mutastic: you are at TriangulateRed: a test from the spec fails. Green: the least code that passes it. Mutate: mutastic runs on the cycle's lines. Triangulate: write each card's test and prove it with verify; one that fails on today's code has found a bug, back to green. Refactor, then commit, which starts the next cycle.Reda test failsGreenleast codeMutaterun --since=HEADTriangulatecard's test, verifyRefactorstructure onlyCommitends the cyclered on today's code: a bug, back to greenthe next cycle starts from the commit
  1. Red
  2. Green
  3. Mutate
  4. Triangulate
  5. Refactor
  6. Commit

The card’s test, with its value checked against the spec: an order of exactly 100 gets 10 off. The card’s name for it ends in 2, as the edge at 30 has the first name, so name it for its edge:

@Test
void loyaltyCreditAtThe100Edge() {
assertEquals(10, new Pricing().loyaltyCredit(100));
}
Terminal window
mutastic verify m809c9c9b samples/pricing
Terminal window
mutastic: m809c9c9b is caught by com.example.pricing.PricingTest#loyaltyCreditAtThe100Edge
Working test-first with mutastic: you are at CommitRed: a test from the spec fails. Green: the least code that passes it. Mutate: mutastic runs on the cycle's lines. Triangulate: write each card's test and prove it with verify; one that fails on today's code has found a bug, back to green. Refactor, then commit, which starts the next cycle.Reda test failsGreenleast codeMutaterun --since=HEADTriangulatecard's test, verifyRefactorstructure onlyCommitends the cyclered on today's code: a bug, back to greenthe next cycle starts from the commit
  1. Red
  2. Green
  3. Mutate
  4. Triangulate
  5. Refactor
  6. Commit

Two ifs are not yet a pattern, so there’s nothing to refactor. Commit.

Working test-first with mutastic: you are at RedRed: a test from the spec fails. Green: the least code that passes it. Mutate: mutastic runs on the cycle's lines. Triangulate: write each card's test and prove it with verify; one that fails on today's code has found a bug, back to green. Refactor, then commit, which starts the next cycle.Reda test failsGreenleast codeMutaterun --since=HEADTriangulatecard's test, verifyRefactorstructure onlyCommitends the cyclered on today's code: a bug, back to greenthe next cycle starts from the commit
  1. Red
  2. Green
  3. Mutate
  4. Triangulate
  5. Refactor
  6. Commit

The same cycle, a tier up. The test:

// 20 off orders of 250 or more.
@Test
void loyalCustomersGetTwentyOffFrom250() {
assertEquals(20, new Pricing().loyaltyCredit(300));
}
Terminal window
mutastic run samples/pricing
Terminal window
mutastic: com.example.pricing.PricingTest#loyalCustomersGetTwentyOffFrom250 fails without any mutant, so mutastic can't judge one.
It failed: expected: <20> but was: <10>
To run without it until it's fixed, list it under exclude-tests: in mutastic.yaml with a reason, or for this run add --exclude-tests=PricingTest#loyalCustomersGetTwentyOffFrom250
Its output is in samples/pricing/build/mutastic/worker.log. To run it alone:
samples/pricing/gradlew -p samples/pricing test --tests 'com.example.pricing.PricingTest.loyalCustomersGetTwentyOffFrom250'
Working test-first with mutastic: you are at GreenRed: a test from the spec fails. Green: the least code that passes it. Mutate: mutastic runs on the cycle's lines. Triangulate: write each card's test and prove it with verify; one that fails on today's code has found a bug, back to green. Refactor, then commit, which starts the next cycle.Reda test failsGreenleast codeMutaterun --since=HEADTriangulatecard's test, verifyRefactorstructure onlyCommitends the cyclered on today's code: a bug, back to greenthe next cycle starts from the commit
  1. Red
  2. Green
  3. Mutate
  4. Triangulate
  5. Refactor
  6. Commit

A third if, above the others:

/// For loyal customers: 5 off orders of 30 or more, 10 off from 100, and 20 off from 250.
public int loyaltyCredit(int total) {
if (total >= 250) {
return 20;
}
if (total >= 100) {
return 10;
}
if (total >= 30) {
return 5;
}
return 0;
}

The whole suite passes.

Working test-first with mutastic: you are at MutateRed: a test from the spec fails. Green: the least code that passes it. Mutate: mutastic runs on the cycle's lines. Triangulate: write each card's test and prove it with verify; one that fails on today's code has found a bug, back to green. Refactor, then commit, which starts the next cycle.Reda test failsGreenleast codeMutaterun --since=HEADTriangulatecard's test, verifyRefactorstructure onlyCommitends the cyclered on today's code: a bug, back to greenthe next cycle starts from the commit
  1. Red
  2. Green
  3. Mutate
  4. Triangulate
  5. Refactor
  6. Commit
Terminal window
mutastic run samples/pricing --since=HEAD
Terminal window
Missed ────────────────────────────────────────────────────────────
❯ src/main/java/com/example/pricing/Pricing.java 1 missed in 1 method
✗ MISSED no test checks loyaltyCredit() at total == 250
┌─ src/main/java/com/example/pricing/Pricing.java:33
│
32 │ public int loyaltyCredit(int total) {
33 │ if (total >= 250) {
│ ^^ >= changed to >
34 │ return 20;
│
= mutant - if (total >= 250) {
+ if (total > 250) {
= operator mutastic/boundary · https://docs.mutastic.io/java/operators/conditions/#boundary
= note: every test calls loyaltyCredit() away from the boundary
inputs seen: 29, 30, 50, 100, 150, 300 (none is 250)
covered by PricingTest#loyalCustomersGetFiveOff, PricingTest#loyalCustomersGetTenOffFrom100, PricingTest#loyalCustomersGetTwentyOffFrom250 and 3 more (all 6 covering tests still pass)
= help: test loyaltyCredit() with total = 250 and assert the result
// in PricingTest
@Test
void loyaltyCreditAtTheTotalEdge2() {
assertEquals(20, new Pricing().loyaltyCredit(250)); // today 20 · with this mutant 10
}
check today's value is the one you intend: the test pins what the code does now
done when mutastic verify m809c9c9b samples/pricing passes
reproduce mutastic run --only=m809c9c9b samples/pricing
or ignore mutastic ignore m809c9c9b --reason="<why>" samples/pricing
Next: mutastic verify <id> samples/pricing · mutastic run --only=<id> samples/pricing · mutastic explain <id> samples/pricing
Classes 0 clean | 1 with misses
Mutants ✓ 3 caught · ✗ 1 missed (4)
Changed 4 lines in 1 file since HEAD
Worst at Pricing.loyaltyCredit() 1 ✗
Test runs 12 (36 without coverage selection)
Start at 03:35:28
Duration 1.8s (setup 0ms, build 515ms, coverage 221ms, mutants 75ms, checking 1.0s, reports 7ms)
Tests 9 on JDK 17 · 1 worker
Reports samples/pricing/build/mutastic/mutation.json · samples/pricing/build/mutastic/mutation.html
Score 75.0% FAIL Below the threshold of 80: 1 missed, 0 not covered.
Working test-first with mutastic: you are at TriangulateRed: a test from the spec fails. Green: the least code that passes it. Mutate: mutastic runs on the cycle's lines. Triangulate: write each card's test and prove it with verify; one that fails on today's code has found a bug, back to green. Refactor, then commit, which starts the next cycle.Reda test failsGreenleast codeMutaterun --since=HEADTriangulatecard's test, verifyRefactorstructure onlyCommitends the cyclered on today's code: a bug, back to greenthe next cycle starts from the commit
  1. Red
  2. Green
  3. Mutate
  4. Triangulate
  5. Refactor
  6. Commit

The same gap, at the new edge. An order of exactly 250 gets 20 off:

@Test
void loyaltyCreditAtThe250Edge() {
assertEquals(20, new Pricing().loyaltyCredit(250));
}
Terminal window
mutastic verify m809c9c9b samples/pricing
Terminal window
mutastic: m809c9c9b is caught by com.example.pricing.PricingTest#loyaltyCreditAtThe250Edge

The id is the last cycle’s: a Java id counts a mutant’s place in its method, and the first >= is at 250 now. It names the mutant there; the one at 100 is another id. See Mutant ids.

Don’t commit yet: this cycle has a refactor in it.

Working test-first with mutastic: you are at RefactorRed: a test from the spec fails. Green: the least code that passes it. Mutate: mutastic runs on the cycle's lines. Triangulate: write each card's test and prove it with verify; one that fails on today's code has found a bug, back to green. Refactor, then commit, which starts the next cycle.Reda test failsGreenleast codeMutaterun --since=HEADTriangulatecard's test, verifyRefactorstructure onlyCommitends the cyclered on today's code: a bug, back to greenthe next cycle starts from the commit
  1. Red
  2. Green
  3. Mutate
  4. Triangulate
  5. Refactor
  6. Commit

The three ifs say one thing three times: from this total up, this credit, the highest tier first. A fourth tier would be a fourth copy. The tiers are data, so the refactor is to hold them as data, a table, read by one loop.

A refactor starts where the cycle is done: the suite passes, and every mutant on the cycle’s lines is caught.

Terminal window
mutastic run samples/pricing --since=HEAD
Terminal window
Classes 1 clean
Mutants ✓ 4 caught (4)
Changed 4 lines in 1 file since HEAD
Test runs 8 (40 without coverage selection)
Start at 19:09:22
Duration 1.6s (setup 0ms, build 411ms, coverage 214ms, mutants 48ms, checking 956ms, reports 7ms)
Tests 10 on JDK 17 · 1 worker
Reports samples/pricing/build/mutastic/mutation.json · samples/pricing/build/mutastic/mutation.html
Score 100.0% PASS Every mutant was caught.

The four are the new tier’s: its >= changed to >, its condition always true and always false, and its 20 returned as 0.

In place of the method, the table and the loop:

/// A loyalty tier: from this total up, this credit.
private record Tier(int from, int credit) {}
/// The loyalty tiers, highest first.
private static final List<Tier> LOYALTY_TIERS =
List.of(new Tier(250, 20), new Tier(100, 10), new Tier(30, 5));
/// For loyal customers: 5 off orders of 30 or more, 10 off from 100, and 20 off from 250.
public int loyaltyCredit(int total) {
for (Tier tier : LOYALTY_TIERS) {
if (total >= tier.from()) {
return tier.credit();
}
}
return 0;
}

And import java.util.List; at the top of the file.

No test changes: a refactor changes structure only, and the tests are what say it kept the behaviour. The whole suite passes.

Terminal window
mutastic run samples/pricing --since=HEAD
Terminal window
Classes 1 clean
Mutants ✓ 6 caught · 2 skipped (2 arid) (8)
Changed 14 lines in 1 file since HEAD
Test runs 10 (60 without coverage selection)
Start at 19:09:24
Duration 1.7s (setup 0ms, build 448ms, coverage 194ms, mutants 64ms, checking 947ms, reports 13ms)
Tests 10 on JDK 17 · 1 worker
Reports samples/pricing/build/mutastic/mutation.json · samples/pricing/build/mutastic/mutation.html
Score 100.0% PASS Every mutant was caught.

The table and the loop are new since HEAD, so these are the new shape’s mutants. The report, mutation.html, shows each on its line:

  • The loop’s hasNext() always false, so no tier is read, and always true, so it reads past the last.
  • total >= tier.from() changed to total > tier.from(): one mutant for the edge of every tier, where the ifs had one each.
  • total >= tier.from() always true, and always false.
  • tier.credit() returned as 0.

The two skipped are the record’s from() and credit(), each returned as 0: code with nothing to decide, which mutastic leaves out as arid.

The table’s numbers have no mutants, as the numbers in the ifs had none: the tests at each tier and its edge are what check them. The loop depends on the table’s order, highest first, as the ifs did on theirs, and the tests pin that too: with the lowest tier first, an order of 150 would get 5, and the test that wants 10 fails.

Every one is caught. The tests still pin every behaviour of the new shape, with none of them changed: the refactor is safe.

The ids aren’t a guide to what changed. Some are the ones before the refactor, on different mutants: md0b32a4f was the if at 250 always true, and is the loop’s hasNext() always true now, as a Java id counts a mutant’s place in its method (see Mutant ids). Compare what’s caught, not ids.

Working test-first with mutastic: you are at CommitRed: a test from the spec fails. Green: the least code that passes it. Mutate: mutastic runs on the cycle's lines. Triangulate: write each card's test and prove it with verify; one that fails on today's code has found a bug, back to green. Refactor, then commit, which starts the next cycle.Reda test failsGreenleast codeMutaterun --since=HEADTriangulatecard's test, verifyRefactorstructure onlyCommitends the cyclered on today's code: a bug, back to greenthe next cycle starts from the commit
  1. Red
  2. Green
  3. Mutate
  4. Triangulate
  5. Refactor
  6. Commit

Then commit. The commit is the next cycle’s HEAD.

Before the refactor, the tests caught every mutant on the cycle’s lines. A mutant missed after it means one of two things. The refactor wasn’t only structure: it added behaviour no test asks for, such as a default for a case the ifs never had. Or the tests stopped pinning something: a test was changed or taken out with the refactor, which a refactor never needs.

Either way, don’t write a test for the miss there and then. Undo that part of the change. If the behaviour is one you want, add it test first, in a cycle of its own, where its test fails until the code does it.