QA
Intermediate LevelSoftware Engineering Principles

What is Consumer-Driven Contract Testing (Pact) and how does it prevent API breakages?

❓ Interview Prompt: Explain Consumer-Driven Contract Testing, Pact framework, mocking provider interactions, generating contract JSON, and avoiding brittle integration tests.

Consumer-Driven Contract Testing (Pact) verifies that microservices can communicate without relying on slow, brittle end-to-end integration environments:

How Pact Works:

1
Consumer Defines Contract: Consumer tests write expected HTTP request/response mocks into a Pact File JSON.
2
Provider Verifies Contract: Provider CI pipeline imports the Pact JSON and replays requests against its real controllers to verify compliance!

Code Implementation Example

typescript
import { Pact } from '@pact-foundation/pact';

// Consumer Defines Contract Expectation
const provider = new Pact({ consumer: 'FrontendApp', provider: 'UserService' });
await provider.addInteraction({
  state: 'user exists with ID 10',
  uponReceiving: 'a request for user 10',
  withRequest: { method: 'GET', path: '/users/10' },
  willRespondWith: { status: 200, body: { id: 10, name: 'Alice' } }
});
💡Defining an API Consumer Contract using the Pact framework.
🎯 Interview Key Takeaway

Use Consumer-Driven Contract Testing (Pact) to verify microservice API contracts without deploying full E2E environments.

#Contract Testing#Pact#API Security#Intermediate
Back to all Software Engineering Principles Questions→

More Software Engineering Principles Interview Questions

Beginner LevelSoftware Engineering Principles

Explain the 5 SOLID Principles of Object-Oriented Software Design with practical refactoring examples.

Beginner LevelSoftware Engineering Principles

What are DRY (Don't Repeat Yourself), KISS (Keep It Simple, Stupid), and YAGNI (You Aren't Gonna Need It)?

Beginner LevelSoftware Engineering Principles

Why is Composition Over Inheritance a fundamental object-oriented design principle?