gradle如何将pact验证结果发布给pact broker?
How to publish pact verification result to pact broker in gradle?
我有一个使用 spring 引导构建的服务,使用 gradle(如果需要,gradle 文件位于底部)和 Junit5。 Whosebug 上的许多问题都是针对 maven 和 Junit4 的,我在翻译答案时遇到了麻烦。我的契约生产者测试根据经纪人的合同正确运行和测试。但是,成功验证的结果不会发送给经纪人。 我需要做什么才能做到这一点?
协议来自 'other Service' 并验证 'my service' 正确 return 三只可爱的猫。这是测试的样子:
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 只猫,测试将失败,因为该协议需要为 return 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')
}
}
}
}
不确定他为什么需要这些信息,这些信息都已经存在于 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相关内容(我参考了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(',')
}
}
您似乎将 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
我有一个使用 spring 引导构建的服务,使用 gradle(如果需要,gradle 文件位于底部)和 Junit5。 Whosebug 上的许多问题都是针对 maven 和 Junit4 的,我在翻译答案时遇到了麻烦。我的契约生产者测试根据经纪人的合同正确运行和测试。但是,成功验证的结果不会发送给经纪人。 我需要做什么才能做到这一点?
协议来自 'other Service' 并验证 'my service' 正确 return 三只可爱的猫。这是测试的样子:
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 只猫,测试将失败,因为该协议需要为 return 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')
}
}
}
}
不确定他为什么需要这些信息,这些信息都已经存在于 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相关内容(我参考了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(',')
}
}
您似乎将 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