Server-Side CCD
In this section we will look at Contract Driven Development using Spring Cloud Contract (SCC) from the perspective of a (Java Spring) server side developer. We will see how to configure a project to read in contracts from a shared repository and have them executed against our api to make sure it is meeting the defined behavior in the contracts.
Configuring the Spring Cloud Contract Plugin
For SCC to validate that your API is matching the behaviors defined in the contracts we wrote in the previous step we must configure its plugin. The plugin has largely already been defined in the pom.xml of the service application, let's step through it below as well as what needs to be changed:
<plugin>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-contract-maven-plugin</artifactId>
<version>${spring-cloud-contract.version}</version>
<extensions>true</extensions>
<configuration>
<baseClassForTests>com.ibm.developer.produce.ContractsBaseClass</baseClassForTests>
<contractsRepositoryUrl>git://https://github.com/wkorando/produce-contracts.git</contractsRepositoryUrl>
<contractDependency>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
</contractDependency>
<contractsMode>LOCAL</contractsMode>
</configuration>
</plugin>baseClassForTests Defines the class Spring Cloud Contract will use when executing the contracts. More on that in the next section.
contractsRepositoryUrl Defines the repository where the contracts are located. You will want to change this to fork you created from the previous page.
contractDependency These are the maven coordinates for where the contracts are stored as well as where the stubs artifact will be placed.
contractsMode This is a bit confusing, but SCC will clone the git contracts repo, which means the contracts will be available locally. After clone the repo, we need to tell SCC to then look for the contracts locally.
Contract Base Class
Full class including imports can be seen here.
As SCC is running the contracts against your API, you will need to have your API simulate what it would be doing as though it where actually running. This is where the Base Class comes into play. In the below base class I have created some mocks that simulate the expected behavior in the contracts.
For example, when findAllProduce is executed a call to GET:api/v1/produce is made which references this method in ProduceController:
In ContractsBaseClass I have a mock that will the return all the produce items the contract is expecting:
Here is what the entire ContractsBaseClass looks like:
Generating Documentation
SCC not only verifies your API matches the behavior defined in the contracts, it can also document your API as well using Spring REST Docs. To do this you will need to confgiure your build file like below:
Additionally in a base class you will need to add the JUnitRestDocumentation JUnit rule and use RestAssuredMockMvc like in the example above:
Finally you will need to add a source folder src/main/asciidoc and create an index.adoc which defines what your documentation looks like. Produce Service already has one created and it looks like this:
Executing mvn package will generate an API document under:target/generated-docs/index.html
Last updated
Was this helpful?