vendredi 12 février 2021

Spring Cloud Contract - Not able to create contract test with the 'fileAsBytes()' response body being different for consumer() and producer() sides

We do have a service that generates pdf documents dynamically(each request - slightly different PDF document). So I have to create a contract test for that service.

Problem statement

STUB side should return a predefined pdf file as a byte array. - OK

SERVER side should not check the response in assertions, or at least check if it matches by some regexp. - NOT WORKING

Here is my contract.groovy

import org.springframework.cloud.contract.spec.Contract
[
    Contract.make {
        request {
            method "GET"
            urlPath("/pdfEndpoint")
        }
        response {
            status 200
            headers {
                contentType(applicationPdf())
            }
            body(value(
                    consumer(fileAsBytes("staticFileToSentOnStubResponse.pdf")),
                    producer(regex(nonBlank())) /*the issue is with that line*/
            ))
        }
    }
]

GenereatedTestClass.java

@Test
public void contractPdf() throws Exception {
    // given:
        MockMvcRequestSpecification request = given();

    // when:
        ResponseOptions response = given().spec(request)
                .get("/pdfEndpoint");

    // then:
        assertThat(response.statusCode()).isEqualTo(200);
        assertThat(response.header("Content-Type")).matches("application/pdf.*");
    // and:
        String responseBody = response.getBody().asString();
        assertThat(responseBody).isEqualTo("^\\s*\\S[\\S\\s]*"); // .isEqualTo() but needed matches()
}

Is there any way to update the groovy file to have in the generated class the folowing assertion

assertThat(responseBody).matches("^\\s*\\S[\\S\\s]*");

instead of

assertThat(responseBody).isEqualTo("^\\s*\\S[\\S\\s]*");

Resources:

  • org.springframework.cloud:spring-cloud-dependencies - Hoxton.SR8
  • java11

Aucun commentaire:

Enregistrer un commentaire