Skip to content
Ameen Abozaid
MID-045

Advanced testing

  • 6 Lessons
  • 3 h practice (estimate)
On this page 8 topics

Overview

Topics

  • Contract testing
  • consumer-driven contracts
  • property-based testing

Learn first

3 h of practice (estimate)

Learning outcomes

  1. Explain why end-to-end tests across many services are slow and brittle.
  2. Write consumer-driven contract tests for HTTP and messaging (Pact or Spring Cloud Contract).
  3. Verify provider contracts in CI and block breaking changes.
  4. Write property-based tests (jqwik) for domain invariants.

Lessons

  1. 01Testing distributed systems
  2. 02Consumer-driven contracts
  3. 03Contracts for messages
  4. 04Contracts in CI
  5. 05Property-based testing
  6. 06Demo: a breaking API change caught by a contract

Lab and exercises

Lab

MID-045-LAB1 · Contracts for Shop

Outcomes: 1–4 · Time: ~3 h Write contracts between orders and inventory (HTTP) and for OrderPlaced (Kafka), verified in both pipelines. Add jqwik property tests for the Order aggregate. Acceptance: renaming a field fails the provider build.

Exercises (assessment: lab plus a test-strategy question)

IDOutcomeTask
MID-045-EX011List the problems of an end-to-end test that places an order across four services; propose which checks move lower.
MID-045-EX022Write a consumer test in orders producing a Pact (or Spring Cloud Contract) for GET /stock/{sku}.
MID-045-EX032Rewrite a contract that asserts a whole response body so it matches only the fields used.
MID-045-EX042Write a message contract for OrderPlaced and verify it in the producer.
MID-045-EX053Add contract verification to the provider pipeline so a failure blocks the build.
MID-045-EX064Write two jqwik properties for the Order aggregate (total equals sum of lines; total never negative).
MID-045-EX071–3Test-strategy question: for a new payments service called by orders, state which tests you write at each level and why.
Show solutions
  • EX01 Slow, brittle, hard to diagnose; move validation to unit tests, interactions to contract tests, DB behavior to Testcontainers; keep one smoke test.
  • EX02 Define the interaction (request, response with sku string and available integer) in the consumer test; the generated file is published.
  • EX03 Replace exact body matching with type matchers on sku and available only.
  • EX04 Message contract with orderId and totalCents; the provider test builds the real event and checks it against the contract.
  • EX05 Pipeline step runs provider verification against published contracts; non-zero exit fails the job.
  • EX06 @Property with a generator of valid orders; assertions as in the lecture; second property: total() >= 0.
  • EX07 Unit tests for payment rules, consumer contract with payments, Testcontainers for persistence, a smoke test; end-to-end only for the happy path.

Outcome 3 is also checked by lab criterion "renaming a field fails the provider build".

Quiz

Outcomes: O1 limits of end-to-end tests · O2 consumer-driven contracts · O3 contracts in CI · O4 property-based tests.

A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.

  1. 1A main weakness of end-to-end tests across many services:O1
  2. 2Most checks should be:O1
  3. 3In a consumer-driven contract, the expectations come from:O2
  4. 4A contract should assert:O2
  5. 5Message contracts apply to:O2
  6. 6The provider verifies contracts:O3
  7. 7The provider renames a field a consumer uses. Result:O3
  8. 8Safe way to remove a field:O3
  9. 9A property-based test:O4
  10. 10Shrinking in jqwik:O4

Practical part

Seven exercises MID-045-EX01 to EX07 (EX07 is the test-strategy question) plus the contracts lab. Pass: at least five exercises and a lab where renaming a field fails the provider build.

Common mistakes

Contracts written by the provider alone; asserting entire payloads.

Assessment

Lab plus test-strategy question.

Career relevance

API evolution (MID-062) and quality strategy at Staff level.

Type to search levels, phases, modules and lessons.