Search code examples
groovyspring-cloud-contract

Groovy and Spring Cloud Contract - Variable in the bodyAsValue


I would like to know how am I supposed to create in Groovy the Json payload with a random value. For example

Contract.make {
    name"MyFirstContract"
    description "A description"

    request {
        method "POST"
        url "/api/team/createTeam"
        headers {
            contentType applicationJson()
            accept applicationJson()
            header"Authorization", execute('bearerOfAccessToken()')
        }
        body """ {
    "authenticatedUserCode":"papas",
    "input": {
        "name":"${execute('generateTeamUniqueName()')}",
        "teamDefinitionName":  "JUNIT TEST NAME",
        "context":"context AHO",
        "description":"team Description",
        "members":
           [
           {"role":"Junit Reviewer",
           "memberType":"USER",
           "reference":"papas"
           },
           {"role":"Junit Observer",
           "memberType":"USER",
           "reference":"papas"
           },
           {"role":"Junit Collaborator",
           "memberType":"USER",
           "reference":"papas"
           },
           {"role":"Junit Reviewer",
           "memberType":"USER",
           "reference":"papas"
           }
           ]     
    } } """
    }

The important part from the previous code is the following that fails during compilation.

        "name":"${execute('generateTeamUniqueName()')}",

with the following error message:

 Execution default-generateTests of goal org.springframework.cloud:spring-cloud-contract-maven-plugin:3.0.2:gener
ateTests failed: expecting '}' or ',' but got current char 'E' with an int value of 69

Is it possible to call a method inside the body or I have to return the ALL the contents of the body through the same method???

Thank you!


Solution

  • You have to call it for the whole body, it won't work for part of it