|
Tip
|
The Accurest project was initially started by Marcin Grzejszczak and Jakub Kubrynski (codearte.io) |
Just to make long story short - Spring Cloud Contract Verifier is a tool that enables Consumer Driven Contract (CDC) development of JVM-based applications. It is shipped with Contract Definition Language (DSL). Contract definitions are used to produce following resources:
-
JSON stub definitions to be used by Wiremock when doing integration testing on the client code (client tests). Test code must still be written by hand, test data is produced by Spring Cloud Contract Verifier.
-
Messaging routes if you’re using one. We’re integrating with Spring Integration, Spring Cloud Stream and Apache Camel. You can however set your own integrations if you want to
-
Acceptance tests (in JUnit or Spock) used to verify if server-side implementation of the API is compliant with the contract (server tests). Full test is generated by Spring Cloud Contract Verifier.
Spring Cloud Contract Verifier moves TDD to the level of software architecture.
Let us assume that we have a system comprising of multiple microservices:
If we wanted to test the application in top left corner if it can communicate with other services then we could do one of two things:
-
deploy all microservices and perform end to end tests
-
mock other microservices in unit / integration tests
Both have their advantages but also a lot of disadvantages. Let’s focus on the latter.
Deploy all microservices and perform end to end tests
Advantages:
-
simulates production
-
tests real communication between services
Disadvantages:
-
to test one microservice we would have to deploy 6 microservices, a couple of databases etc.
-
the environment where the tests would be conducted would be locked for a single suite of tests (i.e. nobody else would be able to run the tests in the meantime).
-
long to run
-
very late feedback
-
extremely hard to debug
Mock other microservices in unit / integration tests
Advantages:
-
very fast feedback
-
no infrastructure requirements
Disadvantages:
-
the implementor of the service creates stubs thus they might have nothing to do with the reality
-
you can go to production with passing tests and failing production
To solve the aforementioned issues Spring Cloud Contract Verifier with Stub Runner were created. Their main idea is to give you very fast feedback, without the need to set up the whole world of microservices.
If you work on stubs then the only applications you need are those that your application is using directly.
Spring Cloud Contract Verifier gives you the certainty that the stubs that you’re using were created by the service that you’re calling. Also if you can use them it means that they were tested against the producer’s side. In other words - you can trust those stubs.
The main purposes of Spring Cloud Contract Verifier with Stub Runner are:
-
to ensure that WireMock / Messaging stubs (used when developing the client) are doing exactly what actual server-side implementation will do,
-
to promote ATDD method and Microservices architectural style,
-
to provide a way to publish changes in contracts that are immediately visible on both sides,
-
to generate boilerplate test code used on the server side.
During the tests you want to have a Wiremock instance / Messaging route up and running that simulates the service Y. You would like to feed that instance with a proper stub definition. That stub definition would need to be valid and should also be reusable on the server side.
Summing it up: On this side, in the stub definition, you can use patterns for request stubbing and you need exact values for responses.
Being a service Y since you are developing your stub, you need to be sure that it’s actually resembling your concrete implementation. You can’t have a situation where your stub acts in one way and your application on production behaves in a different way.
That’s why from the provided stub acceptance tests will be generated that will ensure that your application behaves in the same way as you define in your stub.
Summing it up: On this side, in the stub definition, you need exact values as request and can use patterns/methods for response verification.
Spring Cloud Contract Verifier and Stub Runner are using the following libraries
Below you can find some resources related to Spring Cloud Contract Verifier and Stub Runner. Note that some can be outdated since the Spring Cloud Contract Verifier project is under constant development.
Olga Maciaszek-Sharma talking about Accurest (Spring Cloud Contract Verifier predecessor)
Marcin Grzejszczak and Jakub Kubryński talking about Accurest (Spring Cloud Contract Verifier predecessor)
Here you can find some working samples. Check the readme of each project for more information.
For the time being Spring Cloud Contract Verifier is a JVM based tool. So it could be your first pick when you’re already creating software for the JVM. This project has a lot of really interesting features but especially quite a few of them definitely make Spring Cloud Contract Verifier stand out on the "market" of Consumer Driven Contract (CDC) tooling. Out of many the most interesting are:
-
Possibility to do CDC with messaging
-
Clear and easy to use, statically typed DSL
-
Possibility to copy paste your current JSON file to the contract and only edit its elements
-
Automatic generation of tests from the defined Contract
-
Stub Runner functionality - the stubs are automatically downloaded at runtime from Nexus / Artifactory
-
Spring Cloud integration - no discovery service is needed for integration tests
Here you can find interesting links related to Spring Cloud Contract Verifier:
You can read more about Spring Cloud Contract Verifier by reading the docs
Spring Cloud is released under the non-restrictive Apache 2.0 license, and follows a very standard Github development process, using Github tracker for issues and merging pull requests into master. If you want to contribute even something trivial please do not hesitate, but follow the guidelines below.
Before we accept a non-trivial patch or pull request we will need you to sign the contributor’s agreement. Signing the contributor’s agreement does not grant anyone commit rights to the main repository, but it does mean that we can accept your contributions, and you will get an author credit if we do. Active contributors might be asked to join the core team, and given the ability to merge pull requests.
This project adheres to the Contributor Covenant code of conduct. By participating, you are expected to uphold this code. Please report unacceptable behavior to [email protected].
None of these is essential for a pull request, but they will all help. They can also be added after the original pull request but before a merge.
-
Use the Spring Framework code format conventions. If you use Eclipse you can import formatter settings using the
eclipse-code-formatter.xmlfile from the Spring Cloud Build project. If using IntelliJ, you can use the Eclipse Code Formatter Plugin to import the same file. -
Make sure all new
.javafiles to have a simple Javadoc class comment with at least an@authortag identifying you, and preferably at least a paragraph on what the class is for. -
Add the ASF license header comment to all new
.javafiles (copy from existing files in the project) -
Add yourself as an
@authorto the .java files that you modify substantially (more than cosmetic changes). -
Add some Javadocs and, if you change the namespace, some XSD doc elements.
-
A few unit tests would help a lot as well — someone has to do it.
-
If no-one else is using your branch, please rebase it against the current master (or other target branch in the main project).
-
When writing a commit message please follow these conventions, if you are fixing an existing issue please add
Fixes gh-XXXXat the end of the commit message (where XXXX is the issue number).