Skip to content
This repository has been archived by the owner on Sep 10, 2019. It is now read-only.

Steps to add contract testing (Producer Side)

Renjith G R edited this page Feb 21, 2017 · 1 revision

1) Add spring-cloud-starter-contract-verifier dependency in pom.xml.

<dependency>
    <groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-contract-verifier</artifactId>
	<version>1.0.3.RELEASE</version>
	<scope>test</scope>
</dependency>

2) Add spring-cloud-contract-maven-plugin to pom.xml.

<plugin>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-contract-maven-plugin</artifactId>
    <version>1.0.3.RELEASE</version>
    <extensions>true</extensions>
    <configuration>
	    <packageWithBaseClasses>org.egov.contract</packageWithBaseClasses>
	</configuration>
</plugin>

3) Create the stub definition for the endpoint.

All stub definitions should go in src/test/resources/contracts/ directory. For example src/test/resources/contracts/shouldGenerateCrn.groovy.

package contracts

import org.springframework.cloud.contract.spec.Contract

Contract.make {
    request {
        method 'GET'
        url '/crn'
        headers {
            contentType('application/json')
        }
    }

    response {
        status 200
        body([
               value: '00056-2017-PD'
        ])
        headers {
            contentType('application/json')
        }
    }
}

4) Add ContractVerifierBase.java class

This file should be in /src/test/java/org/egov/contract/ContractVerifierBase.java. For more about how to arrange the base classes read this part from Spring Cloud Contract docs. The following paragraph is taken from the offical docs.

The convention is such that if you have a contract under e.g. src/test/resources/contract/foo/bar/baz/ and provide the value of the packageWithBaseClasses property to com.example.base then we will assume that there is a BarBazBase class under com.example.base package. In other words we take last two parts of package if they exist and form a class with a Base suffix.

package org.egov.contract;

import com.jayway.restassured.module.mockmvc.RestAssuredMockMvc;
import org.egov.domain.service.CrnGeneratorService;
import org.egov.web.controller.CrnController;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import static org.mockito.Mockito.when;


@RunWith(MockitoJUnitRunner.class)
public class ContractVerifierBase {

    @Mock
    CrnGeneratorService crnGeneratorService;

    @InjectMocks
    CrnController crnController;

    @Before
    public void setup() {
        when(crnGeneratorService.generate()).thenReturn("00056-2017-PD");
        RestAssuredMockMvc.standaloneSetup(crnController);
    }

}

5) Generate stubs jar

mvn clean package

This will generate your-project-name-version-SNAPSHOT-stubs.jar

6) Add steps to deploy the jar to Nexus

Add the following to the pom.xml

<distributionManagement>
	<snapshotRepository>
	    <id>repo.egovernments.org</id>
		<name>eGov ERP Snapshots Repository</name>
		<url>http://repo.egovernments.org/nexus/content/repositories/snapshots/</url>
	</snapshotRepository>
</distributionManagement>

Refer settings.xml and build.wkflo from pgr-crn-generation and add them to your project.