The Advantages of Contract Testing: Comparing PACT, Spring Cloud Contract, and Custom Scalable Solutions
Tags: contract, pact, spring, testing
Published: 2023-07-23 01:43:44

Contract testing is a method for verifying the integration of systems by individually examining each application to confirm that contracts and interfaces adhere to the mutually agreed upon terms.
A mock integration point enables the confirmation of a component’s expected behavior without dependence on that component. In a large and intricate application, a trusted integration point may be required by multiple applications. This trust ensures that the integration point’s behaviors are validated by all dependent applications, safeguarding the system from unintended or harmful changes. Thus, a system can be constructed where each component performs a specific task, and the system is protected from accidental or malicious alterations to any component.

## What are the advantages of utilizing contract testing and why is it a preferred choice?

Contract testing is a highly effective technique for:

- Identifying irregularities in consumer workflows and addressing them at early stages
- Detecting service configuration defects that may impact inter-application communication
- Ensuring secure connections even when producers make changes to service configurations, providing guarantees to data consumers
- Allowing producers to perform their tasks without disrupting consumer workflows, which is particularly beneficial during regression testing
- Giving consumers the flexibility to define their requirements and consume only what they need
- Preventing unexpected API communication failures through the use of local or mock implementations, which are more stable, faster, and reliable during the application testing phase
- Facilitating rapid issue detection and resolution

## Under what circumstances is contract testing not recommended?

It is important to consider both the advantages and disadvantages before making a decision.

Here are the main instances when contract testing may not be appropriate:

- Contract tests are not intended to replace Unit or Functional (component) tests
- Contract tests should not be used as a substitute for other types of tests
- If there is little or no communication between teams where contract tests are being set up, this can result in an unreliable and untrustworthy contract
- Using contract tests simply because they provide an additional layer of test coverage or because they are a new and trendy technique is not a valid reason and will not add value to your test flows
- Before making any decisions, it is important to evaluate the business value, estimated time of arrival for implementation, and consider the minimum viable product that can be provided and presented. It is also worth noting that integrating new features or tools requires workshops to onboard teams and users

## Where do contract tests fall within the testing pyramid hierarchy?

![Testing pyramid](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zsigjlboqda7r3c4t4um.jpg)

For a more detailed view, let’s examine the following table:

{% embed https://gist.github.com/EliFuzz/c71b5c7b4ff6322673333aba43761dfc %}

## What are the available tools and options for contract testing?

There are a limited number of battle-tested and production-ready tools available for implementing contract testing. Some options include:

- ![Pact](https://docs.pact.io)
- ![Spring Cloud Contracts](https://spring.io/projects/spring-cloud-contract)

## What factors should be considered when selecting the most suitable contract testing tool for your project?

A brief overview of the capabilities of each tool can aid in making an informed decision:

{% embed https://gist.github.com/EliFuzz/e193e97ac9e140e0b71c3d88bb2676a2 %}

## What are the default approaches or workflows utilized by each tool and how do they differ from one another?

Let’s examine the characteristics of producer-driven contract testing.

![Producer-driven approach](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g9358ndiv9jp2txi2sjt.png)

In a producer-driven approach, the producer generates contracts and passes them on to consumers.

Let’s delve deeper into the workings of the Spring Cloud Contract tool and explore its behind-the-scenes operations.

![Spring Cloud Contract workflow](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/685w9fpnyi00kv1nnvg2.png)

Let’s now examine the workflow of consumer-driven contract testing.

![Consumer-driven approach](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ob3iblqlm9fzdb6apzrs.png)

The following image depicts the functionality of Pact

![Pact workflow](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/33bovmsbitd6k0y4jofq.png)

## How to establish a new contract?

### Add new contract

- Create a new contract
- Store the contract in the contract repository
- Conduct a review of the contract
- Develop consumer-side tests
- Develop producer-side tests
- Integrate the contract into the codebase
- Integrate consumer-side tests into the codebase
- Integrate producer-side tests into the codebase

### Update existing contract

- Modify the existing contract
- Store the updated contract in the contract repository
- Conduct a review of the updated contract
- Implement changes on each consumer’s side
- Implement changes on each producer’s side
- Integrate the updated contract into the codebase
- Integrate changes on the consumer’s side into the codebase
- Integrate changes on the producer’s side into the codebase

## When Should Contract Tests be Executed? Integration into the Development Pipeline

- Build Phase:
  - Compile the code
  - Generate contract files
- Test Phase (conducted in parallel):
  - Execute unit tests
  - Run integration tests
  - Perform contract tests (consider implementing a safe failure backup plan for quick production deployment during incidents)
  - Conduct other automated tests
- Deployment Phase:
  - Deploy the code to the production environment

## Conclusion

In this piece, we delved into the concept of contract testing, weighing its advantages and disadvantages, and provided insights to aid in making informed decisions.

## Next Steps
 
In the [following article](https://dev.to/romanglushach/advanced-contract-testing-solution-with-high-scalability-3e9l), we will demonstrate how to construct a highly scalable solution utilizing pipelines from GitHub or GitLab.