【问题标题】:How to publish pact verification result to pact broker in gradle?如何将协议验证结果发布到 gradle 中的协议代理?
【发布时间】:2020-10-13 12:31:07
【问题描述】:

我有一个使用 gradle(如果需要,位于底部的 gradle 文件)和 Junit5 的 Spring Boot 构建的服务。 stackoverflow 上的许多问题都是针对 maven 和 Junit4 的,我在翻译答案时遇到了麻烦。我的契约生产者测试运行并针对经纪人的合同进行了正确的测试。但是,成功验证的结果不会发送给代理。 我需要做什么才能做到这一点?

协议来自“其他服务”并验证“我的服务”正确返回三只可爱的猫。这是测试的样子:

package com.miau.package;

// imports

@Provider("my_service")
@Consumer("other_service")
@PactBroker(authentication = @PactBrokerAuth(username = "pact",
    password = "${pactPassword}"), //NOSONAR hardcoded password hint no password, just variable
    host = "pact-broker.apps.home.io/", port = "443",
    tags = "sandbox"
)
@SpringBootTest(classes = Application.class,
    properties = { "spring.profiles.active=pact,fake-oauth2", "pact.verifier.publishResults=true" },
    webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Tag("pact")
@ExtendWith({SpringExtension.class})
public class OtherServiceContractVerifier {

    @LocalServerPort
    int port;

    @Autowired
    Application application;

    @BeforeEach
    void before(PactVerificationContext context) {
        HttpTestTarget target = new HttpTestTarget("localhost", port, "/my_service");
        context.setTarget(target);
    }

    @TestTemplate
    @ExtendWith(PactVerificationInvocationContextProvider.class)
    void executePact(PactVerificationContext context) {
        context.verifyInteraction();
    }

    @State("3 cute cats exist")
    public void stateThreecuteCatsExists() {
        // make 3 cute cats exist in database
    }
}

运行./gradlew pactTest pactPublish 时一切正常。测试通过,如果我将状态更改为只有 2 只猫,测试将失败,因为协议要求服务返回 3 只猫。然而,契约经纪人没有显示成功的验证。为此我需要做什么?


我试过了,但是好像错了:

运行./gradlew pactVerify -> “没有配置服务提供者”。 不确定他需要服务提供商做什么,因为测试提供了所有必要的信息。但经过一番谷歌搜索后,我发现了这个并将其添加到我的 build.gradle 中:

Pact {
broker {
    pactBrokerUrl = "https://pact-broker.apps.home.io/"
    pactBrokerUsername = "pact"
    pactBrokerPassword = pactPassword
}
serviceProviders {
    'my_service' {
        fromPactBroker {
            selectors = latestTags('Sandbox')
        }
    }
}
}

不知道他为什么需要这些信息,这一切都已经存在于契约测试中。但至少它改变了./gradlew pactVerify 的输出,现在它列出了合约并尝试验证它只是以Connect to localhost:8080 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused (Connection refused) 失败这似乎是错误的方法,因为它根本不使用现有的测试。我还没有找到如何告诉它,使用现有的OtherServiceContractVerifier 测试。


gradle文件的pact相关内容(我整合了this question的答案,无济于事):

{
    buildscript {
        if (!project.hasProperty('pactPassword')) {
            pactPassword = ''
        }
        if (!project.hasProperty('pactTags')) {
            pactTags = ''
        }
    }
}

plugins {
    id "au.com.dius.pact" version "4.1.7"
}

apply plugin: 'au.com.dius.pact'

task pactTest(type: Test) {
    environment "pactPassword", "$pactPassword"
    description = 'Runs pact tests.'
    group = 'verification'

    useJUnitPlatform()
    outputs.upToDateWhen { false }

    testClassesDirs = sourceSets.pactTest.output.classesDirs
    classpath = sourceSets.pactTest.runtimeClasspath
    shouldRunAfter test
}
check.dependsOn pactTest

dependencies {
    pactTestImplementation "au.com.dius:pact-jvm-consumer-junit5:4.0.10"
    pactTestImplementation "au.com.dius:pact-jvm-consumer-java8:4.0.10"
    pactTestImplementation "au.com.dius:pact-jvm-provider-junit5:4.0.10"
    pactTestImplementation "au.com.dius:pact-jvm-provider:4.0.10"
}

test {
    useJUnitPlatform {
        excludeTags "pact"
    }
}

pact {
    publish {
        pactBrokerUrl = "https://pact-broker.apps.home.io/"
        pactBrokerUsername = "pact"
        pactBrokerPassword = pactPassword
        tags = pactTags.tokenize(',')
    }
}

【问题讨论】:

    标签: java spring-boot junit5 pact


    【解决方案1】:

    您似乎将 Gradle 验证过程 (./gradlew pactVerify) 与 JUnit 之一(例如 OtherServiceContractVerifier)混合在一起。

    两者都可以通过配置

    对于 gradle,请参阅https://docs.pact.io/implementation_guides/jvm/provider/gradle/#project-properties

    对于 JUnit,请参阅 https://docs.pact.io/implementation_guides/jvm/provider/junit/#publishing-verification-results-to-a-pact-broker

    所以设置以下将启用(注意:你应该只从 CI 发布)

    System.setProperty("pact.verifier.publishResults", "true");
    

    使用 JUnit 的示例项目:https://github.com/pactflow/example-provider-springboot

    【讨论】:

    • 谢谢,在这样的代码中设置系统属性时,它可以工作。但是,当使用“-Ppact.verifier.publishResults=true”将其传递给 gradle 任务时,它不起作用。您是否也偶然知道解决方案?
    • 对不起,我不知道。可能是一些 gradle 特定的东西。粗略搜索一下我们的文档有一个类似的例子(见底部docs.pact.io/implementation_guides/jvm/provider/gradle/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-01
    • 2016-02-22
    • 1970-01-01
    • 2015-11-10
    • 2011-12-21
    • 2018-06-21
    • 2020-10-20
    相关资源
    最近更新 更多