Implement consumer-driven contract testing for Java microservices using the Pact framework

Learn how to test Java microservices with consumer-driven contracts in Open Liberty.

Continue reading


Open Liberty

31 Enrolled

At a Glance

Learn how to test Java microservices with consumer-driven contracts in Open Liberty.

With a microservices-based architecture, you need robust testing to ensure that microservices that depend on one another are able to communicate effectively. Typically, to prevent multiple points of failure at different integration points, a combination of unit, integration, and end-to-end tests are used. While unit tests are fast, they are less trustworthy because they run in isolation and usually rely on mock data.
Integration tests address this issue by testing against real running services. However, they tend to be slow as the tests depend on other microservices and are less reliable because they are prone to external changes.

Usually, end-to-end tests are more trustworthy because they verify functionality from the perspective of a user. However, a graphical user interface (GUI) component is often required to perform end-to-end tests, and GUI components rely on third-party software, such as Selenium, which requires heavy computation time and resources.

What is contract testing?
Contract testing bridges the gaps among the shortcomings of these different testing methodologies. Contract testing is a technique for testing an integration point by isolating each microservice and checking whether the HTTP requests and responses that the microservice transmits conform to a shared understanding that is documented in a contract. This way, contract testing ensures that microservices can communicate with each other.
Pact is an open source contract testing tool for testing HTTP requests, responses, and message integrations by using contract tests.
The Pact Broker is an application for sharing Pact contracts and verification results. The Pact Broker is also an important piece for integrating Pact into continuous integration and continuous delivery (CI/CD) pipelines.
The two microservices you will interact with are called system and inventory. The system microservice returns the JVM system properties of its host. The inventory microservice retrieves specific properties from the system microservice.
You will learn how to use the Pact framework to write contract tests for the inventory microservice that will then be verified by the system microservice.

Created by 

The Open Liberty Project team

Estimated Effort

1 hour





Tell Your Friends!

Saved this page to your clipboard!