Spring Cloud Contract

For consumer-driven contracts, Spring also has a project to help the user to easily achieve their contract testing, Spring Cloud Contract (SCC). It basically consists of three major tools:

  • SCC verifier tool
  • SCC WireMock
  • SCC Stub Runner

By default, SCC supports JSON-based stub (WireMock). The verifier should be added on the producer side. You have to define the contract for your URLs. You can keep that in the same repository or have a completely different repository for it. The producer and the consumer both refer to this repository as the common contract. The following is the sample POM file:

<dependencyManagement> 
    <dependencies> 
             <dependency> 
            <groupId>org.springframework.cloud</groupId> 
            <artifactId>spring-cloud-contract-dependencies</artifactId> 
            <version>1.1.1.RELEASE</version> 
            <type>pom</type> 
            <scope>import</scope> 
        </dependency> 
    </dependencies> 
</dependencyManagement> 
<dependencies> 
    <dependency> 
        <groupId>org.springframework.cloud</groupId> 
        <artifactId>spring-cloud-starter-contract-verifier</artifactId> 
        <scope>test</scope> 
    </dependency> 
</dependencies> 
 
<build> 
    <plugins> 
        <plugin> 
            <groupId>org.springframework.cloud</groupId> 
            <artifactId>spring-cloud-contract-maven-plugin</artifactId> 
            <version>1.1.1.RELEASE</version> 
               <extensions>true</extensions> 
            <configuration> 
                <baseClassForTests>in.packt.example</baseClassForTests> 
            </configuration> 
        </plugin> 
    <plugins> 
</build> 

For baseClassForTest, one can use regEx also.

The idea is fairly simple in a Spring contract. Create a contract and an endpoint. Run the test, which automatically generates stub.jar. You can run this stub.jar file in the local Tomcat and the consumer side can easily call and match the result. Anything that has failed in the integration test or previous steps can be caught here. So, it reduces the overall risk of the system going to production.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset