【发布时间】: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