How to Start Writing the Contract Testing for Consumer?

What is Contract Testing?

The contract is a common term used to describe the document which is shared by 2 parties i.e. which provides a legal communication between two or more parties. Conceptually contract testing teaches us the technique which can be used to test the interaction between 2 microservices while testing the integration layer.

Let’s just kick off the first question which comes to mind is How to do contract testing and which tool we should be using?

Let me walk you through all ingredients we need to cook this contract test and what all spices(aka tools) we are going to use in it, will discuss that now. So first we will see what possible tools which are the open-source we have in the market for doing contract testing which is compatible with the most supported language called Java. And We found “PACT” which is a code-first tool used for testing the message interactions between the client(Service A) and server(Service B).

So let’s just discuss our next question: How to write contract tests using PACT then run the tests and discuss benefits?

In order to start writing down the tests, we first understand, here we have split our tests into 2 parts –

  1. Consumer Tests
  2. Provider Tests

Scenario –

Consumer Test(Unit Tests) sends a real/actual request by making a call to an endpoint and receives a minimal answer through Mock Provider and generates a pact file and that pact file is being shared with the Mock Consumer which further sends an expected request to an API provider which further receives an actual response from the API provider which actually verify the pact file at provider side. Both Consumer and Provider share a common contract called PACT which actually verifies the response.

Let’s see how to write a consumer test for creating auth session in the web application:

So, here PROVIDER_ID and CONSUMER_ID will be replaced by the creating auth session service name and web application microservice name respectively.

Writing PACT



username: “”,

password: “”



createAuthSession_dataFile = data.json file

@Pact (provider=PROVIDER_ID, consumer=CONSUMER_ID)

public RequestResponsePact createAuthSession(PactDslWithProvider builder) {

//creating request header

Map<String, String> headers = new HashMap<>();

headers.put(“Content-Type”, “application/json”);

headers.put(“Accept”, “application/json”);

//initializing request body

String body = utils.getDataFromJsonFile(createAuthSession_dataFile);

//initializing and creating response header

Map<String, String> responseHeaders = new HashMap<>();

responseHeaders.put(“Content-Type”, “application/json”);

//building response

PactDslJsonBody publishTokenResponseJsonBody = new PactDslJsonBody();






return builder.uponReceiving(“POST /v1.0/sessions -> 200”)










Read Also:- Guide On Behavior Driven Development Testing

Writing Test for PACT


@PactVerification(value = PROVIDER_ID, fragment = “createAuthSession”)

public void createAuthSession_Test() throws IOException {“Starting creating of Auth session”);

String body = utils.getDataFromJsonFile(createAuthSession_dataFile);“Body for creating Auth session -> “+body);

//creating client

Client client = ClientBuilder.newBuilder().build();

//creating target endpoint

WebTarget target =“/v1.0/sessions”);

//executing request and saving response

Response response = target.request().post(Entity.entity(body, “application/json”));

//converting response to String object

String value = response.readEntity(String.class);

response.close();“Response from creating Auth session -> “+value);


The tests have been written as JUnit tests. So, the classes containing the test cases should have the keyword “Test” in the name

# Adding new contract tests

– Contract tests should be written in — src/com/pact/consumer/

– Methods annotated with @Pact define the pact between the consumer and provider

– Methods annotated with @Test define the contract test, to test the contract between the consumer and provider

Read Also:- Use of Cucumber Automation Testing Tool

# To execute the tests, follow the steps mentioned –

– Open terminal and change directory to the project root directory

– Run the command on terminal — mvn clean test

– Once the tests have been executed, the pact files can be seen in the directory — target/pacts

Originally published at on March 15, 2022.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Devstringx Technologies

Devstringx Technologies


Devstringx Technologies is highly recommended IT company for custom software development, mobile app development and automation testing services