Search code examples
jsonparameter-passingspock

Pass a param to a json in spock test


I am working with the next json in a spock test, where the podCastId is a dynamic value:

private buildPodCast(long podCastId) {
        String jsonString = '''
        {
          "id": ${podCastId},
          "lang": "en",
          "updated": "2019-04-03T19:48:29Z",
          "premium": false,
          "headline": "The Lowe Post",
          "description": "ESPN's Zach Lowe talks to various basketball people about various basketball things.",
          "thumbnails": {
            "light": {
              "href": "http://a.espncdn.com/i/espn/networks_shows/radio/crops/500/the_lowe_post.png",
              "width": 500,
              "height": 500
            }
          }
        }
        '''
        return JsonUtilKt.transformToJsonNode(jsonString)
    }

My problem is that I have to pass the podCastId param in the id value,but in the way that is now, the json is not taking the param value.

I am struggling with this test, any idea? Thanks


Solution

  • I had used the string concatenation:

    private buildPodCast(long podCastId) {
            String jsonString = '''
            {
              "id": ''' + podCastId + ''',
              "lang": "en",
              "updated": "2019-04-03T19:48:29Z",
              "premium": false,
              "headline": "The Lowe Post",
              "description": "ESPN's Zach Lowe talks to various basketball people about various basketball things.",
              "thumbnails": {
                "light": {
                  "href": "http://a.espncdn.com/i/espn/networks_shows/radio/crops/500/the_lowe_post.png",
                  "width": 500,
                  "height": 500
                }
              }
            }
            '''
            return JsonUtilKt.transformToJsonNode(jsonString)
        }