我正在使用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没有采用参数值。
我正在努力进行这项测试,任何想法?谢谢
以下代码也可以正常工作 - 不需要连接,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)
我曾经使用过字符串连接:
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)
}