我已经用spring创建了一个REST服务。现在,我想使用pact和junit 5来测试消费者与提供者之间的通信。我已经有一个运行中的契约经纪人(来自https://github.com/jaimeniswonger/pact-broker-openshift)。消费者测试工作正常,并发布了协议。提供者测试将加载这些并尝试对其进行验证。但是,只有在验证成功的情况下,测试才会上传结果。我尝试使用surefire和failsafe运行测试。
测试代码:
@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT, properties = "server.port=12223")
@Provider("Provider")
@PactBroker
public class ProviderPactTest {
@BeforeEach
void setupTestTarget(PactVerificationContext context) {
context.setTarget((new HttpsTestTarget("localhost", 12223, "/", true)));
}
@TestTemplate
@ExtendWith(PactVerificationInvocationContextProvider.class)
void pactVerificationTestTemplate(PactVerificationContext context) {
context.verifyInteraction();
}
@State({ "state1" })
public void doNothing() {
}
}
契约经纪人属性在pom中定义:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven.surefire.plugin.version}</version>
<configuration>
<systemPropertyVariables>
<pact.provider.version>${project.version}</pact.provider.version>
<pact.verifier.publishResults>true</pact.verifier.publishResults>
<pact.verification.reports>json</pact.verification.reports>
<pactbroker.tags>TAG</pactbroker.tags>
<pactbroker.host>pact-broker.url.com</pactbroker.host>
<pactbroker.port>80</pactbroker.port>
</systemPropertyVariables>
</configuration>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>${junit-platform.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.version}</version>
</dependency>
</dependencies>
</plugin>
是否可以配置测试以通知契约代理验证失败?