Search code examples
spring-bootpact

How to configure the Spring Boot Application to execute the Pact.io provider tests via Gradle


I would like to use pact.io - the consumer driven test framework,
which is there to test api-contracts defined by consumers.

My provider application is written in Spring Boot and Gradle.

Question: How can Gradle be configured to execute the pact tests against the provider application?
The hard part is - to start the application under test, providing the apis, wait until it is up and running and then fire the pact tests against them.


Solution

  • For the following the plugin gradle-execfork-plugin is used.

    buildscript {
        repositories {
            mavenCentral()
            maven {
                url 'https://plugins.gradle.org/m2/'
            }
        }
        dependencies {
            classpath("org.springframework.boot:spring-boot-gradle-plugin:$springbootVersion")
            classpath "gradle.plugin.com.github.hesch:gradle-execfork-plugin:0.1.15"
        }
    }
     
     
    plugins {
        id "au.com.dius.pact" version "4.2.2"
        id 'com.github.hesch.execfork' version '0.1.15'
    }
     
    apply plugin: "org.springframework.boot"
     
    jar {
        baseName = 'account-api'
        version = "$version"
    }
     
    dependencies {
        compile(
                "org.springframework.boot:spring-boot-starter-parent:$springbootVersion",
                "org.springframework.boot:spring-boot-starter-web:$springbootVersion",
                "org.springframework.boot:spring-boot-starter-jersey:$springbootVersion",
                "javax.xml.bind:jaxb-api:2.3.1",
                "org.slf4j:slf4j-api:$slf4jVersion"
        )
     
        testCompile(
                "org.assertj:assertj-core:$assertjVersion",
                "org.springframework.boot:spring-boot-starter-test:$springbootVersion"
        )
    }
     
    task startProvider(type: com.github.psxpaul.task.JavaExecFork) {
        classpath = sourceSets.main.runtimeClasspath
        main = 'com.dius.account.Application'
    //    args = [ '-d', '/foo/bar/data', '-v', '-l', '3' ]
        jvmArgs = ['-Xmx500m', '-Djava.awt.headless=true']
        workingDir = "$buildDir/server"
        standardOutput = "$buildDir/daemon.log"
        errorOutput = "$buildDir/daemon-error.log"
    //    stopAfter = verify
        waitForPort = 8080
        waitForOutput = 'started'
    }
    

    Assuming that the Spring Boot Application is under

    package com.dius.account;
    
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.builder.SpringApplicationBuilder;
    
    @SpringBootApplication
    public class Application {
        public static void main(String[] args) {
            new SpringApplicationBuilder(Application.class).run(args);
        }
    }