ASP.NET Core Web API Interview Question #159

What is contract testing for APIs?

Testing, HttpClient, Integration Testing, Background Services & Deployment Senior Advanced

Quick Interview Answer

Contract testing verifies that an API provider and its consumers agree on request and response expectations. It is particularly useful when services are deployed independently and a provider change could break downstream clients.

Detailed Explanation

Contract testing verifies that an API provider and its consumers agree on request and response expectations. It is particularly useful when services are deployed independently and a provider change could break downstream clients.

Contract tests complement unit and integration tests by focusing on compatibility at service boundaries.

Code Example

// Example contract expectations:
// POST /api/orders
// accepts CreateOrderRequest
// returns 201 + OrderResponse
// returns documented ProblemDetails for invalid input.