Skip to content
Ameen Abozaid
Planned lessonMID-045-L05

Property-based testing

  • Lesson 5 of 6
  • Mid-level
On This Lesson 4 topics

This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.

In this lesson

test domain rules over many generated inputs with jqwik.

Planned topics

  • Example tests check chosen inputs; property tests state a rule that must hold for any valid input.
  • jqwik generates many random inputs and shrinks failures to a minimal case.
  • Good properties for Shop: total equals the sum of its lines; adding then removing an item restores the order; total is never negative.
  • Define generators (@Provide) that produce valid domain objects only.
  • Keep the seed from failures to reproduce them.
Draft outline details

Demonstration

java
@Property
void totalIsSumOfLines(@ForAll("orders") Order o) {
    assertThat(o.total()).isEqualTo(o.lines().stream().mapToLong(Line::cents).sum());
}

Check your understanding

What does shrinking do?

Show answer

Reduces a failing input to the smallest one that still fails.

Practice

Apply this lesson with the module's lab, exercises and quiz.

Lab and exercises Quiz

Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.

Continue to next lesson

Type to search levels, phases, modules and lessons.