What’s up Guys! Welcome to automationcalling.com
This post is a continuation of the blog Contract Test + Spring Boot for Microservices Architecture.
In my previous blog regarding the contract test, there was a challenge to maintain the consumer test pact file under the resource folder which is pre-requisites to run the provider test.
In reality, some challenges are:
- The Consumer and Provider of API may be a different team in the same organization
- The Consumer and Provider of API may be different companies
- The Consumers of API may be consumed different versions and different features
To address all these challenges, PACT Broker is the solution. In case, if you are looking for plug and play option, please visit PACTFLOW, This is fully managed pact broker on SAAS Platform which is licensed.
What are the advantages of PACT Broker?
- Solves the communication problem and share a common understanding and verify results quickly.
- Avoid bottleneck of integration test and Deploy your service quickly with more confidence.
- This will provide API documentation for your Application that’s up-to-date.
- Visualized representation and show you how your real service interact.
- Version management and backward compatibility for multiple consumers.
PACT Broker Features:
- REST API Support
- Automatic documentation for each PACT
- Automatic/Dynamic generation of a network diagram for your deployed microservice architecture.
- Verify results based on Provider Test
- Provides WebHook status that triggers an action when PACT changes. For eg., this can be notified in Slack Channel or email, etc.,
- View Diff for failure results between Consumer and Provider test
In this blog, we are going to take a look at how to set up the PACT Broker using Docker and Run Provider Test.
Pre-requisites:
- Docker community edition version 19 and above must be installed.
- Docker compose file must be ready (This will be covered in the below blog)
Docker Compose File:
Save the below content as docker-compose.yml file and feel free to change ports, username, and password for Postgres and pact broker.
version: '3' services: postgres: image: postgres healthcheck: test: psql postgres --command "select 1" ports: - "5432:5432" environment: POSTGRES_USER: postgres POSTGRES_PASSWORD: postgressdb POSTGRES_DB: postgres broker_app: image: dius/pact-broker ports: - "9080:80" links: - postgres environment: PACT_BROKER_DATABASE_USERNAME: postgres PACT_BROKER_DATABASE_PASSWORD: postgressdb PACT_BROKER_DATABASE_HOST: postgres PACT_BROKER_DATABASE_NAME: postgres
After saving the file, Go to the directory where the file is saved.
Supply the following command:
docker-compose up -d
After the command, the server gets started and display as below snapshot.
Time to Run Consumer Mock Test:
To publish your consumer mock test in Pack broker, you need to add the following plugin in pom.xml
<plugin> <!-- mvn pact:publish --> <groupId>au.com.dius</groupId> <artifactId>pact-jvm-provider-maven_2.11</artifactId> <version>3.5.10</version> <configuration> <pactDirectory>${project.build.directory}/pacts</pactDirectory> <!-- Defaults to ${project.build.directory}/pacts --> <pactBrokerUrl>http://localhost:9080/</pactBrokerUrl> <projectVersion>${project.version}</projectVersion> <!-- Defaults to ${project.version} --> <trimSnapshot>true</trimSnapshot> <!-- Defaults to false --> </configuration> </plugin>
- The very first step is to run all your consumer tests so that the pact files for your tests will be resided under target/pacts/
- The second step is to publish the results to PACT broker which you need to supply the following commands
- mvn pact:publish
After running the maven command, the result should display as below snapshot.
If you look at the above snapshots, the consumer mock test is documented and display on pact broker, this will help to provide better communication regarding how API is to be consumed and provider suppose to get respond.
Time to Run Provider Test Against Pact Broker:
To run the provider test against Back Broker, the following plugin is required on pom.xml
<plugin> <!-- mvn pact:verify --> <groupId>au.com.dius</groupId> <artifactId>pact-jvm-provider-maven_2.11</artifactId> <version>3.5.10</version> <configuration> <serviceProviders> <!-- You can define as many as you need, but each must have a unique name --> <serviceProvider> <name>Provider_Test</name> <!-- All the provider properties are optional, and have sensible defaults (shown below) --> <protocol>http</protocol> <host>localhost</host> <port>8080</port> <path>/</path> <pactBrokerUrl>http://localhost:9080/</pactBrokerUrl> </serviceProvider> </serviceProviders> <pactBrokerUrl/> </configuration> </plugin>
In the above plugin, you need to mention the following:
- Provider Test Name
- Provide Host, Port where your Back Broker is running
Provider Test:
@RunWith(PactRunner.class) @Provider("Provider_Test") @PactFolder("pacts") public class ProviderVerification { @State("Test GET Request") public void toGetState() { } @TestTarget public final Target target = new HttpTarget(8080); }
After that, supply the following command in maven to run provider test
- mvn pact:verify
This will connect the Consumer Mock test on the PACT broker, by using that, it will run the test against real service (Hitting API endpoints) and compare response results between Real Service and Mock Service as shown in the below snapshot.
Refresh the page in PACT broker, which will reflect green status indicates contract test is verified and success
Please use my existing GitHub which has Spring Boot Application and doesn’t have a PACT broker setup. I think it’s good for you guys to play around on this by following the above steps. For an example test, remember you need to start “EmployeeApp” first
Thanks for your time, please do subscribe for more updates!
Leave a Reply