liveBook · Manning

Property-based testing

As explained in 100 Java Mistakes and How to Avoid Them

Property-based testing verifies general behavioral properties by running tests against many generated inputs, often including randomized inputs.

Property-based testing checks general behavioral rules by running randomized operations against a program instead of requiring a separate unit test for every case. Useful properties include producing no unexpected exceptions and ensuring read-only operations do not modify state. section 1.4

A practical property-based testing workflow includes:

  • Generate many random inputs or operation sequences. section 1.4
  • Check an expected property or behavioral bound rather than one fixed output. section 1.4
  • Replay a failing sequence and remove unnecessary operations to produce a smaller reproducible case. section 1.4
  • For comparators, verify the comparator contract with randomized objects; use datasets larger than 31 elements when sorting tests should expose TimSort contract violations. section 7.13
Comparator<Person> PERSON_COMPARATOR =
    Comparator.comparing((Person p) -> p.name)
              .thenComparing(p -> p.age);

A comparator built from ordered keys provides a concrete target for randomized property checks, especially when testing comparison contracts. section 7.13

How 100 Java Mistakes and How to Avoid Them covers it

How deep it goes
Explains generation, property checking, replay, and sequence reduction, but stops short of guaranteeing every bug is found.
Examples
Tests unexpected exceptions, mutation by read-only operations, and comparator ordering using randomized objects and large sorted datasets.
What it assumes
Assumes familiarity with automated testing and expected behavior while teaching randomized operations and property-based checking through examples.
What you can do after
Diagnose/debug reproducible failures with replayed and minimized operation sequences, using jqwik for Java property tests.
How it connects
Connects automated testing with unit testing, integration testing, code coverage, comparator contracts, and TimSort.

Also explained in

Compare how 8 books explain property-based testing →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 Java Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.