Search code examples
spring-cloud-contract

Spring cloud contract error 'expecting to be equal to <generated> closure


I have my contract written in groovy. I have request body with UUID and response is (another, random) UUID.

Contract.make {
    description "Should create configuration"
    request {
        method 'POST'
        url '/configuration'
        body(
       childBranch: $(consumer(regex(uuid()))),       
    )
        headers {
            header (HttpHeaders.CONTENT_TYPE, new NotToEscapePattern(~'application/json;.*charset=UTF-8.*')) 
        }
    }
    response {
        status HttpStatus.CREATED.value()
        body(${consumer(regex(uuid()))})
    }
}

Running gradle task generateContractTests Produces java class that has

    assertThat(responseBody)
.isEqualTo("shouldConfigure$_run_closure1$_closure3$_closure5@6a1d0a91");

Running tests in this class produces

Expecting:
 <""61af388b-d4f8-4eff-ad84-3cb8720138cb"">
to be equal to:
 <"shouldConfigure$_run_closure1$_closure3$_closure5@6a1d0a91">
but was not.

What is wrong with my contract? How to properly write and run contract for my case?


Solution

  • Cześć :) You shouldn't call the NotToEscapePattern really. The problem is that in the response you have body(${consumer(regex(uuid()))}), and you should have body($(producer(regex(uuid()))))