pact-jvm-provider-spring 声明对于 junit5 提供程序测试,不需要使用 spring 库。
但是,@PactBroker 注释取决于系统属性。有没有办法通过 Spring Property Resolver 让这个应用程序属性工作。我尝试创建类似于 SpringEnvironmentResolver.kt 的东西并在上下文设置中使用它。但这没有用。
@Provider("api-provider-app")
@PactBroker
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("test")
public class PactVerificationTest {
@LocalServerPort
private int port;
@Autowired
private Environment environment;
@TestTemplate
@ExtendWith(PactVerificationInvocationContextProvider.class)
void testTemplate(Pact pact, Interaction interaction, HttpRequest request,
PactVerificationContext context) {
context.setTarget(new HttpTestTarget("localhost", port));
context.setValueResolver(new SpringResolver(environment));
context.verifyInteraction();
}
}
我收到以下错误
指定的协议代理主机无效('${pactbroker.host:}')。请提供有效的主机或指定系统属性“pactbroker.host”。
更新 经过更多搜索后发现 setTarget 不起作用,需要将其移至 @BeforeEach 方法。
@BeforeEach
void setContext(PactVerificationContext context) {
context.setValueResolver(new SpringResolver(environment));
context.setTarget(new HttpTestTarget("localhost", port));
}
下面的代码片段帮助它与 @PactFolder 注释一起工作。但是具有属性的@PactBroker 仍然无法正常工作