我使用gradle (如果需要的话底层的gradle文件)和Junit5构建了一个使用spring引导的服务。关于堆栈溢出的许多问题都是针对maven和Junit4的,我在翻译答案时遇到了困难。我的pact生产者测试运行并正确地测试了对经纪人的合同。但是,成功验证的结果不会发送给代理。,我要怎么做才能做到这一点?
该协议来自“其他服务”,并验证“我的服务”是否正确地返回了三只可爱的猫。这就是测试的样子:
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只猫。然而,pact代理没有显示成功的验证。我需要做些什么呢?
我试过了,但似乎错了:
运行./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测试。
与Pact相关的gradle文件的内容(我从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(',')
}
}发布于 2020-10-13 04:31:07
您似乎正在将Gradle验证过程(./gradlew pactVerify)与JUnit one (例如OtherServiceContractVerifier)混合在一起。
两者都可以通过
关于gradle,请参见https://docs.pact.io/implementation_guides/jvm/provider/gradle/#project-properties。
因此,设置以下内容将使之成为可能(注意:您应该只从CI发布)
System.setProperty("pact.verifier.publishResults", "true");使用JUnit:https://github.com/pactflow/example-provider-springboot的示例项目
https://stackoverflow.com/questions/64322037
复制相似问题