Git Product home page Git Product logo

pact-sample's Introduction

Consumer Driven Contract Testing using Pact

Code sample using Pact with Android frontend and Spring Boot backend.

Diagram

Contract

This sample uses Packflow to share and view Pact contracts. View an example pact here.

To fully try out this sample, create an account on Packflow and replace the PACT_BROKER_BASE_URL and PACT_BROKER_TOKEN below with your own.

Test Samples

AddToDoContractTest.kt

GetToDosContractTest.kt

PactVerificationTest.java

Getting Started

  1. Open the To Do Android project in Android Studio

  2. Gradle build task will generate a pact file in app/build/pacts folder based on existing contract tests AddToDoContractTest.kt and GetToDosContractTest.kt.

cd todo-android
./gradlew clean build
  1. Gradle pactPublish task will publish the pact file into a pact broker. Be sure to specify the following environment variables used in app/build.gradle

Environment variables needed for To Do Android. Add these when running pactPublish gradle task.

PACT_BROKER_BASE_URL=https://djoo.pactflow.io
PACT_BROKER_TOKEN=[pactflow-read/write-token]

These env vars are read in the pact.publish section in the app/build.gradle

pact {
    publish {
        pactBrokerUrl = System.getenv('PACT_BROKER_BASE_URL')
        pactBrokerToken = System.getenv('PACT_BROKER_TOKEN')
    }
}

To publish the latest pact file to a pact broker: (Pactflow in this example)

cd todo-android
./gradlew pactPublish
  1. Contract tests in To Do Server project validates the API to the published pact file. Be sure to specify the following environment variable needed to run the PactVerificationTest.java

Environment variables needed for To Do Server. Note the base url has removed the method "https://" portion. Add these when running PactVerificationTest.java

PACT_BROKER_BASE_URL=djoo.pactflow.io
PACT_BROKER_TOKEN=[pactflow-read/write-token]

Flag pact.verifier.publishResults=true specified in todo-server/build.gradle test block publishes verification results to Pactflow after running tests.

To verify, simply run the PactVerificationTest.java

cd todo-server
PACT_BROKER_BASE_URL=[pactflow-url] PACTBROKER_AUTH_TOKEN=[pactflow-read/write-token] ./gradlew test --tests "*PactVerificationTest*"
  1. Sign on to Packflow and confirm a pact is present and verified.

Pact successfully verified

pact-sample's People

Contributors

pivotal-djoo avatar

Stargazers

TJ Maynes avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.