Pact Dsl - provider returning more records than in pact file

こ雲淡風輕ζ 提交于 2019-12-23 03:34:11

问题


I have the following classes:-

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;
}

At Consumer side, I have the LamdaDsl as below:

@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();
}

So in the pact file generated, I will have only one student record.

Now, in the Provider side, with the id as "1", it will give two student records, sample code below:-

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;
}

Now when I run the pact verifier, it fails as it says there are 2 student records in the provider response.

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}]

What change I need to make in the DSL so that it will not look for the actual number of records in the response?


回答1:


Thanks to Ronald for the tip:-

we can use eachLike or minArrayLike for the above case.

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();


来源:https://stackoverflow.com/questions/56713704/pact-dsl-provider-returning-more-records-than-in-pact-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!