我不明白断言在@PactVerification. 对我来说,这更像是一种复杂的说法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");
}
}
Run Code Online (Sandbox Code Playgroud)
所以首先在“createPact”中我们声明
body("{\"condition\": true, \"name\": \"tom\"}")
Run Code Online (Sandbox Code Playgroud)
然后在givenGet_whenSendRequest_shouldReturn200WithProperHeaderAndBody注释中@PactVerification我们这样做
assertThat(response.getBody()).contains("condition", "true", "name", "tom");
Run Code Online (Sandbox Code Playgroud)
但为什么?我们刚刚说过了!据我所知,断言没有出现在生成的 Pact 文件中。它似乎没有任何意义?
除此之外,我认为合同测试的想法是减少集成测试的需求,因为如果测试数据发生变化,它们可能会中断。但这里我们仍然依赖于测试数据。如果Provider中没有“Tom”,那么测试就会失败。我主要想测试合同是否被破坏,而不是测试数据是否改变。
给出的例子是一个人为的例子。在现实生活中使用 Pact,你不会这样做。您PactVerification将调用一个协作方法/类/事物,它负责对您正在模拟的服务进行外部调用。
因此,您的断言就是协作功能正在做什么。
例如。用户服务可能会创建一个具有某些属性的对象,您知道这些属性仅由该外部调用填充。
| 归档时间: |
|
| 查看次数: |
1688 次 |
| 最近记录: |