APIs break silently. A field gets renamed, a type changes, a required field becomes optional — and nobody notices until a downstream service fails in production.

Contract testing catches these issues at the PR stage, before anything ships.

What is Contract Testing?

Contract testing validates that an API's actual behavior matches its documented contract — usually an OpenAPI spec. If the response shape, field types, or status codes deviate from the spec, the test fails.

The Setup

@Test
public void userEndpoint_matchesContract() {
    given()
        .when().get("/api/users/1")
        .then()
        .statusCode(200)
        .body(matchesJsonSchemaInClasspath("schemas/user.json"));
}

Why It Matters

  • Catches breaking changes before merge
  • Documents API behavior automatically
  • Reduces integration bugs between services
  • Builds trust with API consumers
Tip: Run contract tests as a required check on every PR that touches API code.

Conclusion

Contract testing is the cheapest way to prevent the most expensive kind of bug — the one that only shows up in production.