Introduction to Pact Flow

RMAG news

Introduction to Contract Testing

Definition and importance of contract testing: Imagine that you and your friends are planning a party and each one has a task: one will bring the snacks, another the sweets, and another the drinks. To ensure that everything goes well, you make a list of what everyone should bring. “Contract testing” is like this list, but for computer programs that need to talk to each other. It checks that each part of the program is doing exactly what it promised, just like you check that each friend brought what they agreed to for the party. This is important because if someone forgets or brings something wrong, the party might not be as fun. Likewise, if a program doesn’t do what it promises, other programs may stop working correctly.

Differences between contract testing and other types of testing (unit, integration, end-to-end): Imagine you have a Lego. “Unit testing” is like testing each piece of Lego separately to see if they are perfect. “Integration testing” is when you put a few pieces together and check if they fit well and work together. “End-to-end testing” is when you assemble the entire toy and see if it works the way you want, like a walking car. “Contract testing” is as if you had a manual that tells you exactly how each piece should fit together with the others. It checks whether each part of the toy follows the rules in the manual, ensuring that everything will work correctly when you assemble it. Each type of test is important to ensure your toy is perfect and fun to play with!

Benefits of contract testing in microservices architecture: Imagine that you and your friends are building a Lego city, and each one is responsible for a part: one makes the houses, another the streets, and another the parks. For the city to function well, you need to ensure that the streets connect well with the houses and parks. “Contract testing” is like an agreement you make to ensure that each part of the city will fit perfectly with the others. This is super important because, if someone builds a street that doesn’t connect to the houses, people won’t be able to get home! In a microservices architecture, each part of the program is like one of those parts of the city, and contract testing helps ensure that they all work well together, even if they are done by different people or at different times. This makes the city, or the program, work properly and without problems.

Overview of Pact and PactFlow

Improved Collaboration: Enhances communication between development teams.
Reduced Integration Issues: Minimizes problems when integrating different services.
Faster Development Cycles: Speeds up the development process by catching issues early.
Increased Confidence: Provides assurance that services will work together as expected.
PactFlow is like a helper that keeps an eye on these messages to ensure that everyone is complying with what was agreed.
When someone says they are going to bring snacks, Pact checks whether that person actually brought the snacks at the party.

Setting Up the Environment

pom.xml
create an account

Consumer-Driven Contracts

Writing consumer tests with Pact is like making a party wish list, saying exactly what you hope your friends will bring. Imagine that you are organizing the party and want to make sure you get the snacks, sweets and drinks you ordered. Consumer will generate a contract in json format. You write a little code that describes what the response should look like. If the test passes, it means that the other program is fulfilling what was agreed. If it doesn’t pass, you know you need to adjust something.
Writing provider tests with Pact is like ensuring you actually bring what you promised to the party. Imagine that you said that you are going to take a chocolate cake with strawberry frosting. To make sure you don’t forget or take something wrong, you do a check before leaving home: “Do I have the chocolate cake? Yes. Does it have strawberry frosting? Yes.” So, on provider we need to write a code to provider the response for contract.