英文:
How to publish pact verification result to pact broker in gradle?
问题
我有一个使用Spring Boot和Gradle构建的服务(如果需要底部有Gradle文件),以及Junit5。StackOverflow上的许多问题都是关于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 'other Service' and verifies that 'my service' correctly returns three cute cats. This is what the test looks like:
```Java
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
}
}
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 = "https://pact-broker.apps.home.io/"
pactBrokerUsername = "pact"
pactBrokerPassword = pactPassword
}
serviceProviders {
'my_service' {
fromPactBroker {
selectors = latestTags('Sandbox')
}
}
}
}
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('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(',')
}
}
答案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("pact.verifier.publishResults", "true");
Example project that uses JUnit: https://github.com/pactflow/example-provider-springboot
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论