我对 CDC 测试还很陌生,只是迈出了第一步。我已经部署了 Pact-Broker (docker-compose),在 localhost:80 运行。消费者成功将生成的契约发送给代理,但提供者似乎无法获得有效的契约(但这只是假设)。
我正在使用 spring-boot、maven、jUnit5。应用程序测试在 Ubuntu 上运行。 在本地目录中使用 PactFolder 和消费者生成的契约合同会导致成功的测试。
当我切换到 @PactBroker 注释时,提供者能够连接到代理并收到以下响应(我从调试日志中得到它):
{"_links":
{"self":{
"href":"http://localhost/pacts/provider/provider- name/latest","title":"Latest pact versions for the provider provider-name"},
"pb:provider":{"href":"http://localhost/pacticipants/provider-name",
"name":"provider-name"},
"pb:pacts":[
{"href":"http://localhost/pacts/provider/provider-name/consumer/consumer-name/version/1.0.0",
"title":"Pact between consumer-name (v1.0.0) and provider-name",
"name":"consumer-name"}
],
"provider":{
"href":"http://localhost/pacticipants/provider-name",
"title":"provider-name",
"name":"DEPRECATED - please use the pb:provider relation"
},
"pacts":[
{"href":"http://localhost/pacts/provider/provider-name/consumer/consumer-name/version/1.0.0",
"title":"DEPRECATED - please use the pb:pacts relation. Pact between consumer-name (v1.0.0) and provider-name",
"name":"consumer-name"
}
]
}
}
并且测试运行结果如下:
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 9.758 s
FAILURE! - in com.tis.payment.mapper.PaymentMapperApplicationTests
[ERROR] pactVerificationTestTemplate{PactVerificationContext}
Time elapsed: 9.752 s
ERROR!
org.junit.platform.commons.util.PreconditionViolationException:
No supporting TestTemplateInvocationContextProvider provided an invocation context
[INFO]
[INFO] Results:
[INFO]
[ERROR] Errors:
[ERROR] PaymentMapperApplicationTests.pactVerificationTestTemplate » PreconditionViolation
[INFO]
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0
由于使用本地 pact 文件会使测试变绿,我想原因不在我的测试类的代码中,但如果它可能有帮助,我在这里提供它:
@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT,
properties = "server.port=8082")
@Provider("provider-name")
@PactBroker(host = "localhost", port = "80", tags="latest")
//@PactFolder("target/pacts") # uncomment to use local pact files
public class ApplicationTests {
@MockBean
private ProviderServiceClient providerServiceClient;
@BeforeEach
void setupTestTarget(PactVerificationContext context) {
context.setTarget(new HttpTestTarget("localhost", 8082, "/"));
}
@TestTemplate
@ExtendWith(PactVerificationInvocationContextProvider.class)
void pactVerificationTestTemplate(PactVerificationContext context) {
context.verifyInteraction();
}
@State({"valid payment file"})
public void toValid() {
ServiceResponse response = new ServiceResponse();
response.setBatchId("test");
response.setId(1L);
when(providerServiceClient.save(any())).thenReturn(response);
}
@State({"invalid payment file"})
public void toInvalid() {
}
}
由于不能选择使用本地 pact 文件,我真的很想知道如何修复错误,并将感谢任何有用的评论。
maven 协议依赖项:
<dependency>
<groupId>au.com.dius</groupId>
<artifactId>pact-jvm-model</artifactId>
<version>3.5.22</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>au.com.dius</groupId>
<artifactId>pact-jvm-provider-junit5_2.12</artifactId>
<version>3.5.22</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>au.com.dius</groupId>
<artifactId>pact-jvm-consumer-junit5_2.12</artifactId>
<version>3.5.22</version>
<scope>test</scope>
</dependency>
用于发布消费者协议的 Maven 插件:
<plugin>
<groupId>au.com.dius</groupId>
<artifactId>pact-jvm-provider-maven_2.12</artifactId>
<version>3.5.22</version>
<configuration>
<pactBrokerUrl>http://localhost:80</pactBrokerUrl>
<trimSnapshot>true</trimSnapshot>
<!-- Defaults to false -->
</configuration>
</plugin>
契约提供者 docker-compose.yml:
version: '2'
services:
postgres:
image: postgres
restart: always
# healthcheck:
# test: psql postgres --command "select 1" -U postgres
ports:
- "5432:5432"
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
POSTGRES_DB: postgres
broker_app:
image: dius/pact-broker
depends_on:
- postgres
ports:
- "80:80"
links:
- postgres
environment:
PACT_BROKER_DATABASE_USERNAME: postgres
PACT_BROKER_DATABASE_PASSWORD: password
PACT_BROKER_DATABASE_HOST: postgres
PACT_BROKER_DATABASE_NAME: postgres