【发布时间】:2018-05-10 20:45:45
【问题描述】:
我不明白 @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”,则测试将失败。我主要想测试合同是否损坏,而不是测试数据是否已更改。
【问题讨论】:
-
我做了一个介绍 Pact JVM 的小例子,如果有人想看的话:youtube.com/watch?v=F-IUh0M-pu8 描述中的源链接。