An open API service indexing awesome lists of open source software.

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

Awesome Lists containing this project

README

        

## Consumer Driven Contracts Using Pact Framework

#### Projects


Project Description

pact-contract-provider
provider application

pact-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)

![pack broker view](images/pact_broker_view.png)

### 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

![pack broker view 1](images/pact_broker_view.png)

![pack broker view 2](images/contract_inventory_request.png)

![pack broker view 3](images/contract_inventory_response.png)

### 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
```

![pack broker verficiation result](images/pact_broker_verification_result.png)

#### 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.