Search code examples
javatestinggroovyspockwiremock

Use Ternary operator in Groovy ,Spock and Rest Assured Testing - Intergation Test


I have below code where I want to replace values based on where section of the test:

void 'should pass some test() {
  given:
  stubFor(get(urlEqualTo("/someVal/$productOrderId"))
     .willReturn(defaultWiremockResponse()
       .withBody(""" 
          {
            "startDateTime" : ${myTime? "$myTime" : null}
          }
       """
    )
   //...... some when
   //...... some then
   where:
   productOrderId | myTime
   "1"            | "2023-04-23T07:13:15.862Z"  
   "2"            | "null" 
}

This tests works for null but gives error for value like "2023-04-23T07:13:15.862Z"

nested exception is com.fasterxml.jackson.databind.JsonMappingException: Unexpected character ('-' (code 45)): was expecting comma to separate Object entries

I think somehow the "startDateTime" is not getting the value as wrapped around double quotes "" and getting as below

{
  "startDateTime" : 2023-04-23T07:13:15.862Z   // missing quotes around the value
}

and that could be the issue. Can someone help me understand this and fix this error for string interpolation


Solution

  • Try with \ to handle the double quotes

    "startDateTime" : ${myTime? "\"${myTime}\"" : null}