在spock测试中将参数传递给json

问题描述 投票:0回答:2

我正在使用spock测试中的下一个json,其中podCastId是一个动态值:

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)
    }

我的问题是我必须在id值中传递podCastId参数,但是现在的方式是,json没有采用参数值。

我正在努力进行这项测试,任何想法?谢谢

json parameter-passing spock
2个回答
1
投票

以下代码也可以正常工作 - 不需要连接,string interpolation(适用于"""而不适用于''')可以完成这项工作:

def buildPodCast(long podCastId) {
"""
{
    "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
        }
    }
}
"""
}
buildPodCast(2)

0
投票

我曾经使用过字符串连接:

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)
    }
© www.soinside.com 2019 - 2024. All rights reserved.