HLTech / pact-gen

MIT License
14 stars 4 forks source link

PACT Generator

Build Status Coverage Status Scrutinizer Code Quality License: MIT

Table of Contents

  1. Overview
  2. Motivation
  3. Prerequisites
  4. Quick start
  5. Interaction info
  6. Built with
  7. Authors
  8. License

Overview

This repository contains library for generating pact files out of Feign clients.

Motivation

To generate Pact files, tests for each interaction between service consumer and provider must be written. As number of microservices and hence number of interaction grows, writing such tests becomes bigger burden. As pact files are currently the only format of expectations supported by Judge Dredd, automatic generation of Pact files makes it much faster to perform contract tests. What is more developers don't need to update Pact tests each time interactions between services is changed.

Prerequisites

First, the most important thing is that pact-gen is only able to generate pact files out of Feign clients (at the moment - we have support for other http clients in our roadmap).

Our main goal was to create library that doesn't make you write additional boilerplate. However it was not possible to generate fully useful pact files by using only Feign clients features.

Here are conventions that we made:

Quick start

Add pact-gen dependency

Add pact-gen as a dependency to your project:

if you are using gradle add it to build.gradle:

dependencies {
    ...
    compile "com.hltech:pact-gen:version"
}

if you are using maven add it to pom.xml:

  <dependencies>
    <dependency>
      <groupId>com.hltech</groupId>
      <artifactId>pact-gen</artifactId>
      <version>version</version>
    </dependency>
  </dependencies>

After your build tool will be able to resolve that dependency you just have to write simple integration test (you need your application context so your object mapper is properly configured). Such test may look like this:

Generate pact files

package com.hltech.pact.gen.example

import com.fasterxml.jackson.databind.ObjectMapper
import com.hltech.pact.gen.PactGenerator
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest

import spock.lang.Specification

@SpringBootTest
class ContractTestsGenerator extends Specification {

    @Autowired
    private ObjectMapper objectMapper

    private PactGenerator pactGenerator = new PactGenerator()

    def "should generate pact file"() {
        expect:
            pactGenerator.writePactFiles("com.hltech.rest", "consumer-name", objectMapper, new File("build/pacts/"))
    }
}

This test will:

Further steps

You can take profit of your generated pact files by testing via Judge-D - an open-source engine for contract testing.

Interaction info annotation

We have added custom annotation @InteractionInfo that contains information about:

@InteractionInfo(responseStatus = HttpStatus.OK) - will add information to pact file that expected status is 200 OK

@InteractionInfo(responseStatus = HttpStatus.OK, responseHeaders = {"key1=val1", "key2=val2"}) - will add information to pact file, that expected status is 200 OK and that we expect two headers in response: first with name key1 and value val1 and second with name key2 and value val2

@InteractionInfo(responseStatus = HttpStatus.OK, description = "Update test object in the test service") - will add information to pact file, that expected status is 200 OK and that interaction is called Update test object in the test service

@InteractionInfo(responseStatus = HttpStatus.OK, emptyBodyExpected = true) - will add information to pact file that expected status is 200 OK and that (despite declared return type) we expect response to be empty.

@InteractionInfo can be aggregated thanks to @InteractionsInfo annotation - example usage:

@InteractionsInfo({
    @InteractionInfo(responseStatus = HttpStatus.NOT_FOUND),
    @InteractionInfo(responseStatus = HttpStatus.ACCEPTED)
})

such annotations on method will add information to pact file that we expect rest service to be able to return any of 404 NOT FOUND and 202 ACCEPTED HTTP statuses.

Built with

Authors

License

pact-gen is MIT licensed.