如何将 Pact 验证结果发布到 Gradle 中的 Pact Broker?

huangapple go评论124阅读模式
英文:

How to publish pact verification result to pact broker in gradle?

问题

我有一个使用Spring Boot和Gradle构建的服务如果需要底部有Gradle文件),以及Junit5StackOverflow上的许多问题都是关于Maven和Junit4的我很难将答案翻译过来我的Pact生产者测试成功运行并且针对代理正确进行了测试然而成功验证的结果未发送到代理我需要做什么才能实现这一点呢

Pact来自于'other Service',并验证'my service'是否正确返回了三只可爱的猫测试如下所示

```Java
// 代码部分已省略

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


我尝试过,但似乎不正确:

运行./gradlew pactVerify -> "No service providers are configured"。
不确定为什么他需要服务提供者,因为测试提供了所有必要的信息。但在一些谷歌搜索后,我找到了以下内容,并将其添加到了我的build.gradle中:

// 代码部分已省略

不确定为什么他需要这些信息,因为所有信息在Pact测试中已经存在。但至少这改变了./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相关的内容(我从这个问题中整合了答案,但无济于事):

// 代码部分已省略

<details>
<summary>英文:</summary>

I have a service built with spring boot using gradle (gradle file at the bottom if needed) and Junit5. Many of the questions on stackoverflow are for maven and Junit4 and I have trouble translating the answers. My pact producer test runs and tests correctly against the contract on the broker. However the result of the successful verification is not sent to the broker. **What do I have to do for that to happen?**

The pact comes from &#39;other Service&#39; and verifies that &#39;my service&#39; correctly returns three cute cats. This is what the test looks like:

```Java
package com.miau.package;

// imports

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

    @LocalServerPort
    int port;

    @Autowired
    Application application;

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

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

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

When running ./gradlew pactTest pactPublish everything works fine. The test passes and if I change the state to only have 2 cats, the test will fail because the pact requires the service to return 3 cats. However the pact broker does not show the successful verification. What do I need to do for that?


I have tried, but seems to be wrong:

Run ./gradlew pactVerify -> "No service providers are configured".
Not sure what he needs service providers for, as the test gives all the necessary information. But after some googeling I turned up with this and added it to my build.gradle:

Pact {
broker {
    pactBrokerUrl = &quot;https://pact-broker.apps.home.io/&quot;
    pactBrokerUsername = &quot;pact&quot;
    pactBrokerPassword = pactPassword
}
serviceProviders {
    &#39;my_service&#39; {
        fromPactBroker {
            selectors = latestTags(&#39;Sandbox&#39;)
        }
    }
}
}

Not sure why he would need that information, it all already exists in the Pact test. but at least it changes the output of ./gradlew pactVerify, now it lists the contract and attempts to verify it only to fail with Connect to localhost:8080 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused (Connection refused) This seems to be the wrong approach as it doesn't use the existing Test at all. I haven't found how to tell it, to use the existing OtherServiceContractVerifier test.


Pact related content of gradle file (I incorperated the answer from this question to no avail):

{
    buildscript {
        if (!project.hasProperty(&#39;pactPassword&#39;)) {
            pactPassword = &#39;&#39;
        }
        if (!project.hasProperty(&#39;pactTags&#39;)) {
            pactTags = &#39;&#39;
        }
    }
}

plugins {
    id &quot;au.com.dius.pact&quot; version &quot;4.1.7&quot;
}

apply plugin: &#39;au.com.dius.pact&#39;

task pactTest(type: Test) {
    environment &quot;pactPassword&quot;, &quot;$pactPassword&quot;
    description = &#39;Runs pact tests.&#39;
    group = &#39;verification&#39;

    useJUnitPlatform()
    outputs.upToDateWhen { false }

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

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

test {
    useJUnitPlatform {
        excludeTags &quot;pact&quot;
    }
}

pact {
    publish {
        pactBrokerUrl = &quot;https://pact-broker.apps.home.io/&quot;
        pactBrokerUsername = &quot;pact&quot;
        pactBrokerPassword = pactPassword
        tags = pactTags.tokenize(&#39;,&#39;)
    }
}

答案1

得分: 2

以下是翻译好的部分:

你似乎将 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

英文:

You seem to be mixing the Gradle verification process (./gradlew pactVerify) with JUnit one (e.g. OtherServiceContractVerifier).

Both can be configured via

For gradle, see https://docs.pact.io/implementation_guides/jvm/provider/gradle/#project-properties.

For JUnit see https://docs.pact.io/implementation_guides/jvm/provider/junit/#publishing-verification-results-to-a-pact-broker

So setting the following will enable that (note: you should only publish from CI)

System.setProperty(&quot;pact.verifier.publishResults&quot;, &quot;true&quot;);

Example project that uses JUnit: https://github.com/pactflow/example-provider-springboot

huangapple
  • 本文由 发表于 2020年10月13日 00:51:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/64322037.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定