gpt4 book ai didi

pact - 为什么在@PactVerification 中断言?

转载 作者:行者123 更新时间:2023-12-03 22:20:28 27 4
gpt4 key购买 nike

我不明白 @PactVerification 中 assert 的用法.对我来说,它更像是一种复杂的表达方式 1 == 1 .例如:

import static org.assertj.core.api.Assertions.assertThat;
public class PactConsumerDrivenContractUnitTest {
@Rule
public PactProviderRuleMk2 mockProvider
= new PactProviderRuleMk2("test_provider", "localhost", 8080, this);
@Pact(consumer = "test_consumer")
public RequestResponsePact createPact(PactDslWithProvider builder) {
return builder
.given("test GET ")
.uponReceiving("GET REQUEST")
.path("/")
.method("GET")
.willRespondWith()
.body("{\"condition\": true, \"name\": \"tom\"}")
}
@Test
@PactVerification()
public void givenGet_whenSendRequest_shouldReturn200WithProperHeaderAndBody() {
//when
ResponseEntity<String> response
= new RestTemplate().getForEntity(mockProvider.getUrl(), String.class);
//then
assertThat(response.getBody()).contains("condition", "true", "name", "tom");
}
}

所以首先在“createPact”中我们声明
body("{\"condition\": true, \"name\": \"tom\"}")

然后在 givenGet_whenSendRequest_shouldReturn200WithProperHeaderAndBody注释 @PactVerification我们这样做
assertThat(response.getBody()).contains("condition", "true", "name", "tom");

但为什么?我们就这么说了!据我所知,断言没有出现在生成的 Pact 文件中。它接缝填充没有目的?

除此之外,我认为契约测试的想法是减少对集成测试的需求,因为它们可能会中断,例如如果测试数据发生变化。但是这里我们仍然依赖于测试数据。如果 Provider 中没有“Tom”,则测试将失败。我主要想测试契约(Contract)是否被破坏,而不是测试数据是否已更改。

最佳答案

给出的例子是一个人为的例子。在现实生活中使用 Pact,你不会这样做。您的 PactVerification将调用一个协作方法/类/事物,它负责对您正在模拟的服务进行外部调用。

所以你的断言是关于协作功能正在做什么的。

例如。用户服务可能会创建一个具有某些属性的对象,您知道这些属性仅由该外部调用填充。

关于pact - 为什么在@PactVerification 中断言?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47513870/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com