我不明白 assert 在@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");
}
}
所以首先在“createPact”中我们声明
body("{\"condition\": true, \"name\": \"tom\"}")
然后在givenGet_whenSendRequest_shouldReturn200WithProperHeaderAndBody
注释中@PactVerification
我们这样做
assertThat(response.getBody()).contains("condition", "true", "name", "tom");
但为什么?我们就是这么说的!据我所见,断言没有出现在生成的 Pact 文件中。它的接缝填充没有目的?
除此之外,我认为合同测试的想法是减少对集成测试的需求,因为例如如果测试数据发生变化,它们可能会中断。但是这里我们仍然依赖测试数据。如果 Provider 中没有“Tom”,则测试将失败。我主要想测试合同是否损坏,而不是测试数据是否已更改。