Quantcast
Viewing all articles
Browse latest Browse all 129

Contract Tests

Image may be NSFW.
Clik here to view.

Then we will need to add SpringCloudContract to our classpath to be able to declare our contract test in groovy:

Image may be NSFW.
Clik here to view.

And we’ll need to configure spring-cloud-contract-maven-plugin with the name of our base test class:

Image may be NSFW.
Clik here to view.

Then the contract can be written in a declarative form and a test will be generated from this file:

Image may be NSFW.
Clik here to view.

In order to mock the service implementation and only keep the contract beeing tested we must written a default class that will be extended on the generated test:

Image may be NSFW.
Clik here to view.

The generated test will look like this:

Image may be NSFW.
Clik here to view.

This way, on your build pipeline not only the unit tests will run but also these generated contract tests always validating the introduced code. There are a lot of other functionalities on Spring Cloud Contract that can help your development, for example it will generate a “stubbed jar” using wiremock, that will respond accordingly to the groovy declarative tests, in a way that consumers can import this jar an use it for testing their calls to our API without the real call. So on the consumer side a test can be written like this:

Image may be NSFW.
Clik here to view.

Spring Cloud Contract is a great tool to help our contract tests but it is not the only tool available, when leaving the spring framework and diving into java servlet for example, jersey can be used to solve this problems and other tooling can be used to do the same thing, but the most important is that by implementing contract test we can guarantee that our communication with several clients will remain compatible no matter what change is done on our implementation.

GITHUB: Rodrigo Tejada

O post Contract Tests apareceu primeiro em ilegra.


Viewing all articles
Browse latest Browse all 129

Trending Articles