https://github.com/baratharivazhagan/pact-contract-jvm-spring-boot
This project contains samples demonstrating in writing consumer driven contracts using Pact framework
https://github.com/baratharivazhagan/pact-contract-jvm-spring-boot
consumer-driven-contracts contract-testing pact-jvm spring spring-boot
Last synced: about 1 month ago
JSON representation
This project contains samples demonstrating in writing consumer driven contracts using Pact framework
- Host: GitHub
- URL: https://github.com/baratharivazhagan/pact-contract-jvm-spring-boot
- Owner: BarathArivazhagan
- Created: 2017-10-14T16:55:42.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2020-05-09T23:03:30.000Z (almost 5 years ago)
- Last Synced: 2025-03-16T02:04:39.628Z (about 2 months ago)
- Topics: consumer-driven-contracts, contract-testing, pact-jvm, spring, spring-boot
- Language: Java
- Size: 275 KB
- Stars: 6
- Watchers: 0
- Forks: 11
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## Consumer Driven Contracts Using Pact Framework
#### Projects
Project Descriptionpact-contract-provider
provider applicationpact-contract-consumer
consumer application
#### Compatability Matrix
choose the branch based on below maintained versions.
Branch/Version
Spring Boot
Pact Broker
master
2.2.6.RELEASE
4.0.10
v2.0
2.1.5.RELEASE
3.5.7
v1.0
1.5.7.RELEASE
3.5.7
### Run Pact Broker
- Start pact broker as docker containers
```
$ cd contract-pact-springboot
$ docker-compose up -d
```- View Pact broker [url](http://localhost:8500)

### Guide for Pact Consumer
Start with consumer first, As it is consumer driven contract framework.
- Add below dependencies in pom.xml
```xml
au.com.dius
pact-jvm-consumer-junit_2.12
3.6.7
test
```
- Add pact jvm provider maven plugin```xml
au.com.dius
pact-jvm-provider-maven_2.12
3.6.7
${pact.broker.url}
target/pacts
```
- Write the consumer contract test
```java
public class SaveInventoryConsumerTest{
@Rule
public PactProviderRule mockProvider = new PactProviderRule("inventory_provider","localhost", 8080, this);
private RestTemplate restTemplate=new RestTemplate();@Pact(provider = "inventory_provider", consumer = "inventory_consumer")
public RequestResponsePact createPact(PactDslWithProvider builder) {
Map headers = new HashMap<>();
headers.put("Content-Type", MediaType.APPLICATION_JSON_VALUE);PactDslJsonBody bodyResponse = new PactDslJsonBody()
.stringValue("productName", "TV")
.stringType("locationName", "CHENNAI")
.integerType("quantity", 100);return builder
.given("create inventory").uponReceiving("a request to save inventory")
.path("/api/inventory")
.body(bodyResponse)
.headers(headers)
.method(RequestMethod.POST.name())
.willRespondWith()
.headers(headers)
.status(200).body(bodyResponse).toPact();
}
@Test
@PactVerification
public void testCreateInventoryConsumer() throws IOException {
Inventory inventory=new Inventory("TV", "CHENNAI", 100);
HttpHeaders headers=new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity request=new HttpEntity(inventory, headers);
System.out.println("MOCK provider URL"+mockProvider.getUrl());
ResponseEntity responseEntity=restTemplate.postForEntity(mockProvider.getUrl()+"/api/inventory", request, String.class);
assertEquals("TV", JsonPath.read(responseEntity.getBody(),"$.productName"));
assertEquals("CHENNAI", JsonPath.read(responseEntity.getBody(),"$.locationName"));
assertEquals((Integer)100, (Integer)JsonPath.read(responseEntity.getBody(),"$.quantity"));
}}
```- Run maven build to publish the pacts to the pact broker
```sh
cd pact-contract-consumer
mvn clean install pact:publish -Dpact.broker.url=http://localhost:8500
```- Verify the pact broker with the contracts



### Guide for Pact Provider
Now move on to provider side
- Add the below dependencies in pom.xml
```xml
4.0.10
au.com.dius
pact-jvm-provider-junit
${pact.version}
test
au.com.dius
pact-jvm-provider-spring
${pact.version}
test
```- Add the pact provider test case
Test case to test against the pacts from the pact broker to test against the pacts from the pact broker
```java
@RunWith(SpringRestPactRunner.class)
@SpringBootTest(classes=PactContractProviderApplication.class,properties={"spring.profiles.active=test","spring.cloud.config.enabled=false"},webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@ActiveProfiles("test")
@PactBroker(host="localhost",port="8500")
@Provider("inventory_provider")
public class InventoryProviderTest {
@MockBean
private InventoryService inventoryService;@TestTarget
public final Target target = new HttpTarget(9050);
@State(value="create inventory")
public void createInventoryState() throws Exception{Inventory inventory=new Inventory("TV", "CHENNAI", 100);
when(inventoryService.saveInventory(any(Inventory.class))).thenReturn(inventory) ;
}
}
```- Run maven build at the provider side
```sh
cd pact-contract-provider
mvn clean install -Dpact.verifier.publishResults=true
(or)
mvn test -Dpact.verifier.publishResults=true
```
#### Notes:
1. Using @PactBroker(host="localhost",port="8500") to define the pact broker host and port.
2. Using ```SpringRestPactRunner``` to load the spring container using @SpringBootTest.
3. Starting the application server using the line ```new HttpTarget(9050)```.
4. Mentioning Pact broker url and Pact directory is key to generate pacts at the consumer side.