我不明白断言在@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", …Run Code Online (Sandbox Code Playgroud) 下面的 Json 包含三件事,我不知道如何在基于 lambda 的 DSL 中为 Pact 建模(并且我无法通过阅读https://github.com/DiUS/pact-jvm上提供的示例来弄清楚)/tree/master/pact-jvm-consumer-java8)。
Json 对象由 3 个属性组成;“Inventory”包含一个数组(只有一个元素长)和两个简单的键值对。
1) 如何使用基于 lambda 的 DSL 声明命名数组?
Inventory 数组中的第一个(也是唯一一个)对象由两个命名对象“Car”和“Camera”组成。
2) 如何使用基于 lambda 的 DSL 声明命名对象?
在“Camera”对象中,有一个名为“Conditions”的数组,其中包含两个字符串值。
3) 如何使用基于 lambda 的 DSL 在命名数组中声明两个示例字符串值?
{
"Inventory":[
{
"Car":{
"gearbox":"automatic",
"ProductId":30212
},
"Camera":{
"EndPrice":1235,
"Conditions":[
"FaultyButtons",
"FaultyCasing"
],
"ModelId":"650"
}
}
],
"IsSuccess":true,
"Info":"Ok"
}
Run Code Online (Sandbox Code Playgroud)