-
Notifications
You must be signed in to change notification settings - Fork 59
Steps to add contract testing (Producer Side)
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-contract-verifier</artifactId>
<version>1.0.3.RELEASE</version>
<scope>test</scope>
</dependency>
<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>
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')
}
}
}
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 thepackageWithBaseClasses
property tocom.example.base
then we will assume that there is aBarBazBase
class undercom.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);
}
}
mvn clean package
This will generate your-project-name-version-SNAPSHOT-stubs.jar
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.