Pact Dsl-提供程序返回的记录多于pact文件中的记录

时间:2019-06-22 08:01:51

标签: pact pact-jvm

我有以下课程:-

public class Student {

  private String id;
  private String firstName;
  private String lastName;
  private int age;
}

public class DepartmentResponse {
private String id;
private String name;
List<Student> students;
}

在消费者方面,我有如下LamdaDsl:

@Pact(consumer = "StudentServiceConsumer1")
public RequestResponsePact createPact(PactDslWithProvider builder) {
    Map<String, String> headers = new HashMap();
    headers.put("Content-Type", "application/json");
    final DslPart actualPactDsl = LambdaDsl.newJsonBody((bodyDsl) -> {
        bodyDsl
                .stringType("id", "1")
                .stringType("name","Dep 1")
                .array("students",(stud) ->{
                    stud.object((s1->{
                        s1.stringType("id","1")
                           .stringType("firstName","John")
                           .stringType("lastName","Smith")
                           .numberType("age",21);
                    }));
                });

    })
            .build();
  return builder
            .given("Department 1 exist")
            .uponReceiving("A request for DepartmentResponse Object with 
  Id 1")
            .path("/department/1")
            .method("GET")
            .willRespondWith()
            .status(200)
            .headers(headers)
            .body(actualPactDsl).toPact();
}

所以在生成的协定文件中,我只有一个学生记录。

现在,在提供者一侧,id为“ 1”的它将提供两个学生记录,示例代码如下:-

public  DepartmentResponse getDepartmentById(String id){

  Student student1 = new Student();
  student1.setId("1");
  student1.setAge(23);
  student1.setFirstName("John");
  student1.setLastName("Smith");

  Student student2 = new Student();
  student2.setId("2");
  student2.setAge(21);
  student2.setFirstName("Adam");
  student2.setLastName("Zamba");

  DepartmentResponse department = new DepartmentResponse();
  department.setId(id);
  department.setName("Dep 1");
  department.setStudents(Arrays.asList(student1,student2));

  return department;
}

现在,当我运行契约验证程序时,它失败了,因为它说提供者响应中有2条学生记录。

java.lang.AssertionError: 
0 - $.students -> [{mismatch=Expected a List with 1 elements but received 2 elements, diff=    {
+        "id": "1",
        "firstName": "John",
        "lastName": "Smith",
-        "id": "1",
+        "age": 23
+    },
+    {
+        "id": "2",
+        "firstName": "Adam",
+        "lastName": "Zamba",
        "age": 21}]

我需要对DSL进行哪些更改,以便它不会在响应中查找实际的记录数?

1 个答案:

答案 0 :(得分:1)

感谢罗纳德的提示:-

在上述情况下,我们可以使用eachLike或minArrayLike。

final DslPart actualPactDsl = LambdaDsl.newJsonBody((bodyDsl) -> {
            bodyDsl
                    .stringType("id", "1")
                    .stringType("name","Dep 1")
                    .minArrayLike("students",1,(stud) ->{
                        stud
                            .stringType("id","1")
                               .stringType("firstName","John")
                               .stringType("lastName","Smith")
                               .numberType("age",21);
                    });

        }).build();