如何配置 Spring 引导应用程序以通过 Gradle 执行 Pact.io 提供程序测试

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

我想使用 pact.io - 消费者驱动的测试框架,
这是为了测试 api-消费者定义的合同。

我的提供商应用程序是用 Spring Boot 和 Gradle 编写的。

问题: 如何配置 Gradle 以针对提供者应用程序执行 pact 测试?
困难的部分是 - 启动被测应用程序,提供 apis,等到它启动并 运行 然后针对它们启动协议测试。

以下使用插件gradle-execfork-plugin

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'
}

假设 Spring 引导应用程序在

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);
    }
}

Pact提供消费者测试库,无需启动服务器

至于提供商,您的 @SpringBootTest 注释就可以了。

您正在使用的 gradle 插件是消费者发布契约所必需的