Embedded expressions with karate DSL don't replace value in json

1.5k Views Asked by At

so basically I am just starting with karate testing framework and and probably missing something really simple, but I cant seem to get an embedded expression to be resolved properly. If I have a feature file like so that does the same thing a couple ways:

Feature: Test Service

  Background:
    * url 'http://testurl:8080'
    * def localDateTime = Java.type('java.time.LocalDateTime')

  Scenario: Successful request

    * def createDateTime = LocalDateTime.now()
    * def testRequest =
    """
    {
      createDateTime: "#(createDateTime)",
      expiryDateTime:"#(localDateTime.now().plusMinutes(5))"
    }
    """
    * print testRequest
    * set testRequest.createDateTime = createdTime
    * print testRequest

Then when it gets to the print lines, I get output like this where the values are empty js objects {}

16:43:28.580 [main] INFO com.intuit.karate - [print] {"createDateTime":{},"expiryDateTime":{}}16:43:28.586 [main] DEBUG com.jayway.jsonpath.internal.PathCompiler - Using cached path: $true[]

Also, I can see where it appears to be setting the path for the first print statement like so:

16:32:30.612 [main] DEBUG com.jayway.jsonpath.internal.CompiledPath - Evaluating path: $['createDateTime'] 16:32:30.613 [main] DEBUG com.jayway.jsonpath.internal.JsonReader - Set path $['createDateTime'] new value 2017-09-14T16:32:30.566 16:32:30.629 [main] DEBUG com.jayway.jsonpath.internal.CompiledPath - Evaluating path: $['expiryDateTime'] 16:32:30.629 [main] DEBUG com.jayway.jsonpath.internal.JsonReader - Set path $['expiryDateTime'] new value 2017-09-14T16:37:30.621

Can anybody please explain to me why I am unable to get the actual dates inserted into the testRequest?

Thank you.

1

There are 1 best solutions below

1
On BEST ANSWER

I think your problem will be solved if you convert those date objects to strings.

* def LocalDateTime = Java.type('java.time.LocalDateTime')
* def createDate = LocalDateTime.now() + ''
* def expiryDate = LocalDateTime.now().plusMinutes(5) + ''
* def testRequest = { createDateTime: '#(createDate)', expiryDateTime: '#(expiryDate)' }
* print karate.pretty(testRequest)

The above is working for me and this is the output:

06:11:47.010 [main] INFO  com.intuit.karate - [print] {
  "createDateTime": "2017-09-15T06:11:46.983",
  "expiryDateTime": "2017-09-15T06:16:46.990"
}