Skip to content
mutastic

Java edition

Arid rules in practice

Your report command fetches the day’s sales on a worker thread, waits for it, builds the text, logs how long it all took, records that time for a dashboard, and prints the report. A mutastic run skips some of that as arid and counts the rest, and the line between them isn’t always where you’d draw it. This guide takes one such class through a run: which mutants are skipped and why, which are counted, how to check the skips, and how to bring back the one your tests rely on.

samples/sales in mutastic’s repository:

/** What the report command prints. */
public static void print(Supplier<List<Long>> sales, Metrics metrics)
throws InterruptedException {
System.out.println(of(sales, metrics));
}
public static String of(Supplier<List<Long>> sales, Metrics metrics) throws InterruptedException {
long start = System.nanoTime();
var fetched = new AtomicReference<List<Long>>(List.of());
var worker = new Thread(() -> fetched.set(sales.get()));
worker.start();
worker.join(5_000);
List<Long> amounts = fetched.get();
var text = new StringBuilder(amounts.size() * 8);
long total = 0;
for (long amount : amounts) {
text.append(amount).append('\n');
total += amount;
}
text.append("total ").append(total);
long took = (System.nanoTime() - start) / 1_000_000;
log.info("summed {} amounts in {} ms", amounts.size(), took);
metrics.record("summary.ms", took);
return text.toString();
}

Its one test checks what the command prints, from a source that takes a moment, as a real one does:

@Test
void printsEachAmountThenTheTotal() throws Exception {
var printed = new ByteArrayOutputStream();
var console = System.out;
System.setOut(new PrintStream(printed, true, StandardCharsets.UTF_8));
try {
Summary.print(slowly(List.of(12L, 30L)), (name, value) -> {});
} finally {
System.setOut(console);
}
assertEquals(
"12\n30\ntotal 42" + System.lineSeparator(), printed.toString(StandardCharsets.UTF_8));
}
Terminal window
mutastic run samples/sales
Terminal window
Missed ────────────────────────────────────────────────────────────
✗ src/main/java/com/example/sales/Summary.java:35 tests pass even when - becomes + in of() m3b95337d
✗ src/main/java/com/example/sales/Summary.java:35 tests pass even when / becomes * in of() m5c07bce9
✗ src/main/java/com/example/sales/Summary.java:37 no test checks the effect of calling record() in of() m1b0c9fa7
Next: mutastic verify <id> samples/sales · mutastic run --only=<id> samples/sales · mutastic explain <id> samples/sales
Classes 0 clean | 1 with misses
Mutants ✓ 10 caught · ✗ 3 missed · 3 skipped (3 arid) (16)
Worst at Summary.of() 3 ✗
Test runs 13 (13 without coverage selection)
Start at 19:06:07
Duration 7.4s (setup 3ms, build 4.5s, coverage 324ms, mutants 1.4s, checking 1.1s, reports 52ms)
Tests 1 on JDK 17 · 1 worker
Reports samples/sales/build/mutastic/mutation.json · samples/sales/build/mutastic/mutation.html
Score 76.9% FAIL Below the threshold of 80: 3 missed, 0 not covered.

The test catches most of it: a removed start() or join, a removed append, the total’s +. Some mutants are skipped as arid, and the time worked out at the end is missed. Here is each, in turn.

Removing the log.info call is skipped as arid: only writes output, by the output rule. The report comes out the same without it, and a test that read the log line would break the next time someone reworded it. Keep this skip unless the log is the product, an audit log say, and a test reads it.

With --operators=stronger, strings get mutants too. The format string, "summed {} amounts in {} ms", only goes into the log call, so it’s skipped by the same rule:

Terminal window
mutastic explain ma5436468 samples/sales
Terminal window
SKIPPED arid: only writes output
┌─ src/main/java/com/example/sales/Summary.java:36
│
35 │ long took = (System.nanoTime() - start) / 1_000_000;
36 │ log.info("summed {} amounts in {} ms", amounts.size(), took);
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ "summed {} amounts in {} ms" changed to ""
37 │ metrics.record("summary.ms", took);
│
= mutant - log.info("summed {} amounts in {} ms", amounts.size(), took);
+ log.info("", amounts.size(), took);
= operator mutastic/string-constant · https://docs.mutastic.io/java/operators/library/#string-constant
reproduce mutastic run --only=ma5436468 samples/sales
= arid mutastic:output: Code that only writes output: a log line, or text printed to the
console, and a value worked out for nothing else.
docs: https://docs.mutastic.io/reference/arid/#output
to run it, add to mutastic.yaml:
rules:
- mutate: com.example.sales.Summary#of
arid: [mutastic:output]
reason: <why a test should check it>

The values after it don’t change that: the string still goes nowhere but the log. Nor would a value worked out only for the log, (System.nanoTime() - start) / 1_000_000 passed straight to log.info, or through a local nothing else reads: its mutants would be skipped as arid: only feeds a log line, by the same rule.

Here the time goes somewhere else too:

long took = (System.nanoTime() - start) / 1_000_000;
log.info("summed {} amounts in {} ms", amounts.size(), took);
metrics.record("summary.ms", took);

So the - becoming + and the / becoming * are counted, and missed, and so is removing metrics.record: the value reaches more than the log, and a dashboard reads the metric. mutastic skips a value only when every use of it, on every path, is a log call’s or a print’s argument.

If a test should check the metric, write it: a Metrics that keeps what it’s given, and an assertion on it. If nobody tests the timing, because it’s checked in the dashboards, say so yourself. mutastic ignore records the decision, with its reason, in .mutastic/ignores.yaml, for review with the code:

Terminal window
mutastic ignore m3b95337d samples/sales --as=telemetry --reason="timing only, checked in dashboards"
Terminal window
mutastic: m3b95337d is ignored: timing only, checked in dashboards
recorded in samples/sales/.mutastic/ignores.yaml: commit it, as a decision to review
to undo: mutastic ignore --remove m3b95337d samples/sales

Do the same for the others. --as=telemetry counts them apart in the summary, and each ignore lapses when its line changes, so a value later used for more is counted again.

amounts.size() * 8 sizes the StringBuilder, and its * becoming / is skipped as arid: only sizes a collection's first allocation, by the capacity rule. A builder grows as it needs to, so the text is the same either way. Keep it.

Removing worker.join(5_000) isn’t arid, though it has a time limit: without it, of reads the sales before the worker has fetched them, a race, and the test catches it. The waits rule skips only a removed Thread.sleep or TimeUnit.sleep, which changes when the code gets somewhere, not what it finds there.

The limit itself, 5_000, counts too: a shorter one gives up on the worker sooner, and of reads the sales unfinished. Only how long a sleep waits is arid, by the wait-times rule.

Removing System.out.println(...) in print is skipped as arid: only writes output too, by the same rule as the log line. Here, though, the output is the product: it’s what the report command is for, and the test reads it.

Terminal window
mutastic explain m03c37b45 samples/sales
Terminal window
SKIPPED arid: only writes output
┌─ src/main/java/com/example/sales/Summary.java:18
│
17 │ throws InterruptedException {
18 │ System.out.println(of(sales, metrics));
│ ^^^^^^^ call removed
19 │ }
│
= mutant - System.out.println(of(sales, metrics));
+ of(sales, metrics);
= operator mutastic/remove-void-call · https://docs.mutastic.io/java/operators/calls/#remove-void-call
reproduce mutastic run --only=m03c37b45 samples/sales
= arid mutastic:output: Code that only writes output: a log line, or text printed to the
console, and a value worked out for nothing else.
docs: https://docs.mutastic.io/reference/arid/#output
to run it, add to mutastic.yaml:
rules:
- mutate: com.example.sales.Summary#print
arid: [mutastic:output]
reason: <why a test should check it>

The audit runs every arid mutant anyway, to show which skips your tests would catch:

Terminal window
MUTASTIC_ARID=audit mutastic run samples/sales
Terminal window
Mutants ✓ 10 caught · ✗ 3 missed · 3 skipped (3 arid) (16)
Arid audit 3 ran anyway: 1 caught (1 by an assertion), 2 missed, 0 not covered; checked in Summary.print()
a test checks what they do: to run them, add to mutastic.yaml
rules:
- mutate: com.example.sales.Summary#print
arid: [mutastic:output]
reason: <why a test should check it>

One of them was caught by an assertion, in Summary.print(): a test checks what that code does, so the skip is hiding a contract. The audit says how: bring the rule back there, for that method and that rule only, with a reason, in mutastic.yaml:

rules:
- mutate: com.example.sales.Summary#print
arid: [mutastic:output]
reason: the report is what the command prints, and scripts read it

The run after it runs the println’s mutant, and the test catches it. The log line and the buffer stay skipped, and the three ignored mutants are counted apart:

Terminal window
note: 3 ignores in com.example.sales.Summary#of: one rule says it once, skip: com.example.sales.Summary#of in mutastic.yaml
Classes 1 clean
Mutants ✓ 11 caught · 5 skipped (2 arid, 3 ignored as telemetry) (16)
Test runs 11 (11 without coverage selection)
Start at 19:06:36
Duration 4.9s (setup 3ms, build 1.4s, coverage 727ms, mutants 1.3s, checking 1.5s, reports 50ms)
Tests 1 on JDK 17 · 1 worker
Reports samples/sales/build/mutastic/mutation.json · samples/sales/build/mutastic/mutation.html
Score 100.0% PASS Every mutant was caught. ▲ 23.1 vs last run (same code)
  • Skips that are right for this code, kept: the log line and its text, the buffer’s size.
  • A timing the log and a dashboard use, which no test checks, ignored with a reason, by sort.
  • The one skip that hid a contract, the command’s output, brought back for one method and one rule, with the test that catches it.

Each rule, what it can hide and the setting that brings it back are in Arid rules.