Consumer Driven Contract Testing Java

Pact is a consumer-first code tool for testing contracts and is typically used by developers and testers who program. The contract is generated when performing automated consumer testing. A great advantage of this model is that only the parts of the communication that are actually used by consumers are tested. This, in turn, means that any vendor behavior not used by current consumers is free to change without interrupting testing. Integration testing solves this problem by running tests on real services. However, they tend to be slow because testing depends on other microservices and is less reliable because it is vulnerable to external changes. We pass the name of the provider, host, and port on which the server simulation (created from the contract) is started. As we can see, the POST request response code is equal to 201 – exactly as defined in the Covenant Treaty. The second step in our contract review is to create a test for the supplier using a fictitious customer based on the contract. The Maven version generates a file named test_consumer-test_provider.json in the target/mypacts folder that contains the query and response structure: after creating the InventoryPactIT class.java and replacing the pom.xml file, Open Liberty automatically reloads its configuration. For this configuration to work, we need to place the test_consumer-test_provider.json file in the pacts folder of our REST service project.

Indeed, the tests are carried out against the contract. Let`s see how to write consumer and provider tests with Pact JVM 🚀 When we trigger the build, the spring-cloud-contract-maven-plugin automatically generates a stub in the target/stubs directory. The stub is the wireMock generated contracts that are converted to JSON format by the spring-cloud-contract-maven-plugin. We can refer to the image above for the directory structure of the supplier project. This stub is used to make fun of the supplier`s site. Contractual testing fills in the gaps between the gaps in these different testing methods. Contract testing is a technique for testing an integration point by isolating each microservice and verifying that http requests and responses transmitted by the microservice meet a common understanding documented in a contract. In this way, contract testing ensures that microservices can communicate with each other. The consumer is a service that uses the API exposed by the producer or listens to a message from the producer (e.B. Kafka Consumer consuming the message from Kafka Topic) RestController bean PersonRestController.java the bean PersonService when a REST request is received for a person with the ID. contract-rest-service/src/main/java/hello/PersonRestController.java Typically, there is a contract between a consumer (e.B a customer who wants to receive data) and a provider (e.B an API on a server that provides the data required by the client).

In microservices architectures, traditional client and server terms are not always appropriate, e.B. when communicating through message queues. For this reason, we adhere to consumers and suppliers in this documentation. If the communication between the consumer service test and the manufacturer`s contract is incorrect, the tests will fail and the problem will have to be resolved before a new production change is made. Pact is an open source contract review tool for testing HTTP message requests, responses, and integrations using contract testing. If we make changes on the supplier`s side without updating or informing the consumer`s page, this will result in a result where the contract is broken and the test fails. You must first create the service that creates the contract. It is a regular Spring Boot application that provides a very simple REST service. The rest of the service simply returns a Person object in JSON. When we trigger the build, the spring-cloud-contract-maven-plugin automatically generates a test class called EmployeeTest, which extends our BaseClass and inserts it into /target/generated-test-sources/contracts.

Each of these tests uses a mockup of its contract-based counterpart, which means that adding a new interaction by the consumer creates a new Pact file and the same must be verified by the provider in the Pact broker. To run the stubs, we need to add a dependency to the consumer side of create-employee-application below. You configure two microservices, one of which provides its contract and the other consumes that contract to ensure that integration with the contract provider`s service conforms to specifications. In the future, if the manufacturer`s service contract changes, consumer service testing will not detect the potential incompatibility. In the Lambda DSL above, we used numberType and booleanType generates a matcher that checks only the type, while numberValue and booleanValue insert a concrete value into the contract. The use of igniters reduces the close coupling between consumers and producers. Values such as 2000, 8, etc. are examples of values returned by the dummy server. The next part is testing: unlike a schema or specification (for example, OAS), which is a static artifact that describes all the possible states of a resource, a Pact contract is applied by running a collection of test cases, each of which describes a single concrete request-response pair – Pact is actually “contract for example”.

In this step, the test results should be GREEN when you run the tests, indicating that the REST controller is aligned with the contract and that you have a fully functional service. Whenever the Pact documentation refers to “contractual tests”, it refers to “integration contract tests” as described earlier on this page. Since the application is developed with Spring Boot in Java, we prefer pact-JVM for consumer-oriented contract testing. Pact-JVM provides a DSL for contract definition. In addition, pact-JVM offers good integration with test frameworks such as JUnit, Spock, ScalaTest, etc., as well as with building tools such as Maven, Gradle and sbt. Let`s take an example, we make some changes on the controller`s page, for example by changing the URL.B to get the employee`s profile. If we do not inform the consumer, he will continue to try the old URL and he will get 404 Not Found instead of the desired answer, which will lead to the failure of the contract. Finally, we specify the states in the contract that we want to test: the annotation @PactVerification is responsible for starting the HTTP service. In the test, we just need to send the GET request and make sure that our response is in accordance with the contract. Model class Person.java contract-rest-service/src/main/java/hello/Person.java The contract between the inventory and the microservices of the system is called a pact. .