Skip to content

Quickstart

A test that checks one example passes whenever the code is right for that example. This page has you add a property beside one, watch it find an input the example missed, and keep that input as a test that runs every time. It takes about five minutes, on sample-frameworks/quickstart, a small project in proptastic’s repository, which gets proptastic from Maven Central (Install adds it to your own build). In a clone of proptastic:

Terminal window
cd sample-frameworks/quickstart

Sort.java sorts a list of numbers:

src/main/java/Sort.java
import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;
class Sort {
/** The numbers, smallest first. */
static List<Integer> sort(List<Integer> numbers) {
return new ArrayList<>(new TreeSet<>(numbers));
}
}

Its test checks one example, and the example passes. Beside it is a property:

src/test/java/SortTest.java
import static io.proptastic.Proptastic.forAll;
import static io.proptastic.Proptastic.integers;
import static io.proptastic.Proptastic.lists;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.List;
import org.junit.jupiter.api.Test;
class SortTest {
@Test
void sortsThreeNumbers() {
assertEquals(List.of(1, 2, 3), Sort.sort(List.of(3, 1, 2)));
}
@Test
void sortKeepsSize() {
var ints = lists(integers());
forAll(ints).check(xs -> assertEquals(xs.size(), Sort.sort(xs).size()));
}
}

lists(integers()) says which inputs to try: lists of integers, named ints. .check(…) says what must hold for every one of them: sorting a list keeps its size. The property is a plain JUnit @Test, and the lambda’s parameter, xs, is the name a failure gives the input. Assert as your tests already do: the AssertJ tabs show each step with AssertJ, which the project has as a test dependency.

Terminal window
./gradlew test
./gradlew test
SortTest > sortKeepsSize() FAILED
io.proptastic.core.PropertyFailure: expected: <2> but was: <1>
sortKeepsSize failed after 1 try (seed 0x5eed)
shrunk counterexample: xs = [0, 0]
pin it: .example(List.of(0, 0))
rerun: PROPTASTIC_SEED=0x5eed ./gradlew test --tests 'SortTest.sortKeepsSize'
Caused by:
org.opentest4j.AssertionFailedError: expected: <2> but was: <1>

The example passes and the property fails. Each run draws new inputs, so yours may fail after a different number of tries, with its own seed. This one is the run with PROPTASTIC_SEED=0x5eed.

  • Your assertion’s message comes first: a list of 2 came back with 1 in it.
  • The header names the property, says how many tries it took to fail, and gives the seed, which decides what’s drawn.
  • The shrunk counterexample is the smallest input that still fails. proptastic found a failing list among random ones, then made it smaller until it couldn’t. [0, 0] has a number twice, and the sort goes through a TreeSet, which keeps each number once. The example had no repeats, so it couldn’t see that.
  • pin it is a line to paste into the property, so this input runs every time.
  • rerun runs this one property again with the same seed, so it draws the same inputs.

Paste the pin it line after forAll(…):

src/test/java/SortTest.java
@Test
void sortKeepsSize() {
var ints = lists(integers());
forAll(ints)
.example(List.of(0, 0)) // the input proptastic found
.check(xs -> assertEquals(xs.size(), Sort.sort(xs).size()));
}

Run the tests again:

./gradlew test
SortTest > sortKeepsSize() FAILED
io.proptastic.core.PropertyFailure: expected: <2> but was: <1>
sortKeepsSize failed on example 1 of 1
counterexample: xs = [0, 0]
Caused by:
org.opentest4j.AssertionFailedError: expected: <2> but was: <1>

It fails on the example, before any random input is drawn. An example runs first, every time, whatever the seed, and it isn’t shrunk: it’s already the input you chose. So there’s no seed to copy and nothing to pin. The bug is now a test.

Sort the list without a set, so a number that’s there twice stays twice:

src/main/java/Sort.java
import java.util.List;
class Sort {
/** The numbers, smallest first. */
static List<Integer> sort(List<Integer> numbers) {
return numbers.stream().sorted().toList();
}
}

Run the tests again, and both pass. Keep the example: if a later change brings the bug back, it fails on [0, 0] first, whatever the random inputs are.

./gradlew test runs every test, and a setting such as a new seed doesn’t make Gradle run an unchanged task again. The proptastic command runs your properties through your build all the same, only them once it knows which they are, and says what each found. On this project, before the fix:

Terminal window
proptastic run
Failed ────────────────────────────────────────────────────────────────
✗ SortTest#sortKeepsSize counterexample ca97c5d54
expected: <2> but was: <1>
sortKeepsSize failed after 1 try (seed 0x2fc03457e2e24c2)
shrunk counterexample: xs = [0, 0]
pin it: .example(List.of(0, 0))
rerun: proptastic run --only='SortTest#sortKeepsSize()' --seed=0x2fc03457e2e24c2
Next: proptastic run --only='SortTest#sortKeepsSize()' --seed=0x2fc03457e2e24c2
Properties ✗ 1 failed (1)
Ran ./gradlew test --rerun
Reports build/proptastic/report/report.json · build/proptastic/report/index.html
registered 1 property; later runs run only those

Its Next: line runs that property alone, from the same seed, and proptastic example pins its counterexample for you: Keeping an example. proptastic run has every option.

A property says one true thing about your code. A model says everything the code should do, in a few lines written to be obviously right. proptastic runs the model beside your code and stops at the first step where they disagree, shrunk to the shortest run that shows it. Follow a model does this for a counter, with nothing to install.

  • Cheatsheet: what you’ve just used and the rest of the API on one page, to keep open while you write properties, or print.
  • Test first: a worked example: write the property before the code, and let each counterexample say what to build next.
  • With your test framework: the same property in JUnit 4, TestNG, Kotlin and others, and how each shows a failure.
  • Domains: the inputs you can draw, from numbers to your own records.
  • A bank account: one property built up in steps, until it checks a rule after every operation.
  • Shrinking and Seeds and replay: how proptastic gets from a random failure to [0, 0], and how a seed draws it again.

Privacy