How to Start Writing the Contract Testing for Consumer? – Devstringx

Back to Blog
Contract Testing

How to Start Writing the Contract Testing for Consumer? – Devstringx

What Is Contract Testing?

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

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 open-source we have in the market for doing contract testing which is compatible with the most support 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).

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


Consumer Test (Unit Tests) sends a real/actual request by making a call to an endpoint. It receives a minimal answer through the Mock provider and generates a pact file 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 verifies the pact file at provider side. Both Consumer and Provider share a common contract called PACT which actually verifies the response.

Write a consumer test for creating auth session in the web application

So, here PROVIDER_ID and CONSUMER_ID will replace 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 the response

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

//converting response to a 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 the terminal and change the 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


  • What is contract testing?

Contract testing is a method of testing the interactions between different software systems to ensure that they are working correctly and as expected. In contract testing, a “contract” define between the systems, specifying the expected input and output for each interaction. The systems then test against this contract to verify that they are functioning correctly.

  • What are contract testing tools?

Contract testing tools are software or services that use to automate the process of contract testing. These tools can help define the contract between systems, execute tests against the contract, and report on the results of the tests.

  • What is consumer contract testing?

Consumer contract testing is a specific type of contract testing that use to test the interactions between a consumer system and a provider system. In this case, the consumer system is the system that is making requests to the provider system, and the provider system is the system that is responding to those requests.

  • What are software contract tests?

Software contract tests are tests that specifically design to verify that software systems are functioning according to the defined contract between them. These tests can use to ensure that systems are sending and receiving data correctly and that they are handling errors and exceptions as expected.

  • What is the Pact tool for contract testing?

The Pact tool is a popular open-source tool for contract testing. Its design helps developers define and test the interactions between different software systems. It can use to verify that the systems are functioning correctly and as expected. The Pact tool is available in a variety of programming languages. It can integrate into a continuous integration/continuous delivery (CI/CD) pipeline to facilitate automated testing.

To read more software testing-related articles and information from us here at Devstringx, then we have a lot to choose from for you.

Share this post

Back to Blog