Skip to content

Migrating from example tests

You don’t have to throw your example tests away to use properties. The cases you picked are good cases: each is something someone thought worth checking. A property keeps them and adds every input you didn’t think of. This guide takes a parameterized test and turns it into a property, one step at a time.

A shop writes prices in cents and shows them as 1.50. This is how it formats them:

Money.java
/** 150 cents as "1.50". */
static String format(long cents) {
return cents / 100 + "." + cents % 100;
}

And its tests: three hand-picked amounts, each with what it should show, through JUnit’s @ParameterizedTest:

MoneyTest.java
@ParameterizedTest
@CsvSource({"150, 1.50", "1099, 10.99", "99, 0.99"})
void formatsAPrice(long cents, String shown) {
assertEquals(shown, Money.format(cents));
}

They pass. The bug is in every amount whose cents are under 10: 105 cents shows as 1.5, which reads back as 150. None of the three picked amounts has cents under 10.

Each case pairs an input with an expected output, and a property can’t know the expected output for an input it drew. So look for what’s true of every case: a price, read back, is the cents it was made from. That’s a round trip, and it needs no expected output at all.

The three amounts become examples, with .example(…), and run first, every time, before any random try:

MoneyTest.java
@Test
void aPriceReadsBackAsItsCents() {
forAll(longs().atLeast(0).atMost(1_000_000))
.example(150L)
.example(1_099L)
.example(99L)
.check(cents -> assertEquals(cents, Money.parse(Money.format(cents))));
}

The property finds the amounts the cases missed, and shrinks to the smallest:

Terminal window
expected: <1> but was: <10>
aPriceReadsBackAsItsCents failed after 1 try (seed 0x5eed)
shrunk counterexample: cents = 1
pin it: .example(1L)
rerun: PROPTASTIC_SEED=0x5eed ./gradlew test --tests 'MoneyTest.aPriceReadsBackAsItsCents'

One cent shows as 0.1, which reads back as ten. The fix pads the cents to two digits.

A round trip can’t say that 1.50 is the right way to show 150 cents: a format that’s wrong the same way both ways would pass it. So keep the example test for what it says exactly, and the property for the rule. To share the cases, put them in one list: @FieldSource reads it into a parameterized test, and .examples(…) reads it into a property:

SharedExamplesTest.java
static final List<Long> amounts = List.of(150L, 1_099L, 99L);
@ParameterizedTest
@FieldSource("amounts")
void showsTwoDecimals(long cents) {
assertTrue(Money.format(cents).matches("[0-9]+[.][0-9]{2}"));
}
@Test
void anyAmountReadsBack() {
forAll(longs().atLeast(0).atMost(1_000_000))
.examples(amounts)
.check(cents -> assertEquals(cents, Money.parse(Money.format(cents))));
}

A case added to the list then runs in both.

When a property fails, the failure ends with a pin it line. Paste it after forAll(…) and that input becomes an example too: the next case your example tests would have needed, added for you. Reading a failure says what each line of a failure is.

  • A table of cases with a rule behind it: this guide. Look for a round trip, an invariant, or a simpler version to compare with; Patterns has the common ones.
  • A case that’s the rule itself, such as “an empty basket costs nothing”: keep it as an example test, or pin it as an example of the property that covers it.
  • A test of exact output, a format a person or a file depends on: keep the example test, as above, and add the property beside it.

Privacy