如何在 Jmeter 中使用 groovy 验证嵌套的 JSON 响应?

How can I verify a nested JSON response using groovy in Jmeter?

我正在通过 Jmeter 测试我的 restful API。我得到 Json 多次包含相同的键,例如 ("id",MI,DS etc )。我无法通过以下代码进行验证:

String expected="Iam writing following complete JSON Response as it is"
JSONObject data = getRESTData({vars.get("APIResponsedata")});
    JSONAssert.assertEquals(expected, data, false);

-其中 APIResponsedata 是 Json 通过正则表达式获得的响应 提取器

响应对象:

{
    "success": true,
    "error": null,
    "data": {
        "Months": [
            {
                "Id": 0,
                "MDN": "January 2020",
                "H": [
                    {
                        "MI": 100,
                        "MN": "API Automation Testing Lower the better Metric-Sum",
                        "GL": "B",
                        "DS": "%",
                        "RT": "s"
                    },
                    {
                        "MI": 101,
                        "MN": "API Automation Testing Higher the better Metric-Sum",
                        "GL": "A",
                        "DS": "%",
                        "RT": "s"
                    },
                    {
                        "MI": 102,
                        "MN": "API Automation Testing Higher the better Metric-AVG",
                        "GL": "A",
                        "DS": "s",
                        "RT": "a"
                    },
                    {
                        "MI": 103,
                        "MN": "API Automation Testing Lower the better Metric-Number-AVG",
                        "GL": "B",
                        "DS": "s",
                        "RT": "a"
                    }
                ],
                "T": [
                    {
                        "MI": 100,
                        "VL": "0.20",
                        "NA": false,
                        "PTG": 0,
                        "S": null
                    },
                    {
                        "MI": 101,
                        "VL": "0.20",
                        "NA": false,
                        "PTG": 0,
                        "S": null
                    },
                    {
                        "MI": 102,
                        "VL": "0.00",
                        "NA": true,
                        "PTG": 0,
                        "S": null
                    },
                    {
                        "MI": 103,
                        "VL": "0.20",
                        "NA": false,
                        "PTG": 0,
                        "S": null
                    }
                ],
                "MR": [
                    {
                        "MI": 100,
                        "VL": "0.00",
                        "NA": true,
                        "PTG": 0,
                        "S": null
                    },
                    {
                        "MI": 101,
                        "VL": "0.00",
                        "NA": true,
                        "PTG": 0,
                        "S": null
                    },
                    {
                        "MI": 102,
                        "VL": "0.00",
                        "NA": true,
                        "PTG": 0,
                        "S": null
                    },
                    {
                        "MI": 103,
                        "VL": "0.00",
                        "NA": true,
                        "PTG": 0,
                        "S": null
                    }
                ],
                "WR": [
                    {
                        "WN": 1,
                        "DT": [
                            {
                                "MI": 100,
                                "VL": "0.00",
                                "NA": true,
                                "PTG": 0,
                                "S": null
                            },
                            {
                                "MI": 101,
                                "VL": "0.00",
                                "NA": true,
                                "PTG": 0,
                                "S": null
                            },
                            {
                                "MI": 102,
                                "VL": "0.00",
                                "NA": true,
                                "PTG": 0,
                                "S": null
                            },
                            {
                                "MI": 103,
                                "VL": "0.00",
                                "NA": true,
                                "PTG": 0,
                                "S": null
                            }
                        ],
                        "SD": "29/12/2019",
                        "ED": "04/01/2020"
                    },
                    {
                        "WN": 2,
                        "DT": [
                            {
                                "MI": 100,
                                "VL": "0.00",
                                "NA": true,
                                "PTG": 0,
                                "S": null
                            },
                            {
                                "MI": 101,
                                "VL": "0.00",
                                "NA": true,
                                "PTG": 0,
                                "S": null
                            },
                            {
                                "MI": 102,
                                "VL": "0.00",
                                "NA": true,
                                "PTG": 0,
                                "S": null
                            },
                            {
                                "MI": 103,
                                "VL": "0.00",
                                "NA": true,
                                "PTG": 0,
                                "S": null
                            }
                        ],
                        "SD": "05/01/2020",
                        "ED": "11/01/2020"
                    },
                    {
                        "WN": 3,
                        "DT": [
                            {
                                "MI": 100,
                                "VL": "0.00",
                                "NA": true,
                                "PTG": 0,
                                "S": null
                            },
                            {
                                "MI": 101,
                                "VL": "0.00",
                                "NA": true,
                                "PTG": 0,
                                "S": null
                            },
                            {
                                "MI": 102,
                                "VL": "0.00",
                                "NA": true,
                                "PTG": 0,
                                "S": null
                            },
                            {
                                "MI": 103,
                                "VL": "0.00",
                                "NA": true,
                                "PTG": 0,
                                "S": null
                            }
                        ],
                        "SD": "12/01/2020",
                        "ED": "18/01/2020"
                    },
                    {
                        "WN": 4,
                        "DT": [
                            {
                                "MI": 100,
                                "VL": "0.00",
                                "NA": true,
                                "PTG": 0,
                                "S": null
                            },
                            {
                                "MI": 101,
                                "VL": "0.00",
                                "NA": true,
                                "PTG": 0,
                                "S": null
                            },
                            {
                                "MI": 102,
                                "VL": "0.00",
                                "NA": true,
                                "PTG": 0,
                                "S": null
                            },
                            {
                                "MI": 103,
                                "VL": "0.00",
                                "NA": true,
                                "PTG": 0,
                                "S": null
                            }
                        ],
                        "SD": "19/01/2020",
                        "ED": "25/01/2020"
                    }
                ],
                "SD": "01/01/2020",
                "ED": "31/01/2020"
            }
        ],
        "MND": "2019-12-29T00:00:00Z",
        "MXD": "2020-01-25T00:00:00Z"
    }
}

我收到以下错误:

 Assertion error: true
    Assertion failure: false
    Assertion failure message: javax.script.ScriptException: 
     org.codehaus.groovy.control.MultipleCompilationErrorsException: startup 
    failed:
    Script50.groovy: 5: expecting anything but ''\n''; got it anyway @ line 5, column 18.
       "{"success":true,
                        ^

Iam writing following complete JSON Response as it is

这是你的问题,你要么需要使用单引号,例如:

String expected=`Iam writing following complete JSON Response as it is`

或用反斜杠转义所有双引号,例如:

String expected = "{\n" +
        "    \"success\": true,\n" +

或者为了更好的可读性使用 triple-single-quoted-string 比如:

String expected = '''
{
  "success": true,
  "error": null,
  "data": {
    "Months": [
      {
        "Id": 0,
        "MDN": "January 2020",
        "H": [
          {
            "MI": 100,
            "MN": "API Automation Testing Lower the better Metric-Sum",
            "GL": "B",
            "DS": "%",
            "RT": "s"
          },
          {
            "MI": 101,
            "MN": "API Automation Testing Higher the better Metric-Sum",
            "GL": "A",
            "DS": "%",
            "RT": "s"
          },
          {
            "MI": 102,
            "MN": "API Automation Testing Higher the better Metric-AVG",
            "GL": "A",
            "DS": "s",
            "RT": "a"
          },
          {
            "MI": 103,
            "MN": "API Automation Testing Lower the better Metric-Number-AVG",
            "GL": "B",
            "DS": "s",
            "RT": "a"
          }
        ],
        "T": [
          {
            "MI": 100,
            "VL": "0.20",
            "NA": false,
            "PTG": 0,
            "S": null
          },
          {
            "MI": 101,
            "VL": "0.20",
            "NA": false,
            "PTG": 0,
            "S": null
          },
          {
            "MI": 102,
            "VL": "0.00",
            "NA": true,
            "PTG": 0,
            "S": null
          },
          {
            "MI": 103,
            "VL": "0.20",
            "NA": false,
            "PTG": 0,
            "S": null
          }
        ],
        "MR": [
          {
            "MI": 100,
            "VL": "0.00",
            "NA": true,
            "PTG": 0,
            "S": null
          },
          {
            "MI": 101,
            "VL": "0.00",
            "NA": true,
            "PTG": 0,
            "S": null
          },
          {
            "MI": 102,
            "VL": "0.00",
            "NA": true,
            "PTG": 0,
            "S": null
          },
          {
            "MI": 103,
            "VL": "0.00",
            "NA": true,
            "PTG": 0,
            "S": null
          }
        ],
        "WR": [
          {
            "WN": 1,
            "DT": [
              {
                "MI": 100,
                "VL": "0.00",
                "NA": true,
                "PTG": 0,
                "S": null
              },
              {
                "MI": 101,
                "VL": "0.00",
                "NA": true,
                "PTG": 0,
                "S": null
              },
              {
                "MI": 102,
                "VL": "0.00",
                "NA": true,
                "PTG": 0,
                "S": null
              },
              {
                "MI": 103,
                "VL": "0.00",
                "NA": true,
                "PTG": 0,
                "S": null
              }
            ],
            "SD": "29/12/2019",
            "ED": "04/01/2020"
          },
          {
            "WN": 2,
            "DT": [
              {
                "MI": 100,
                "VL": "0.00",
                "NA": true,
                "PTG": 0,
                "S": null
              },
              {
                "MI": 101,
                "VL": "0.00",
                "NA": true,
                "PTG": 0,
                "S": null
              },
              {
                "MI": 102,
                "VL": "0.00",
                "NA": true,
                "PTG": 0,
                "S": null
              },
              {
                "MI": 103,
                "VL": "0.00",
                "NA": true,
                "PTG": 0,
                "S": null
              }
            ],
            "SD": "05/01/2020",
            "ED": "11/01/2020"
          },
          {
            "WN": 3,
            "DT": [
              {
                "MI": 100,
                "VL": "0.00",
                "NA": true,
                "PTG": 0,
                "S": null
              },
              {
                "MI": 101,
                "VL": "0.00",
                "NA": true,
                "PTG": 0,
                "S": null
              },
              {
                "MI": 102,
                "VL": "0.00",
                "NA": true,
                "PTG": 0,
                "S": null
              },
              {
                "MI": 103,
                "VL": "0.00",
                "NA": true,
                "PTG": 0,
                "S": null
              }
            ],
            "SD": "12/01/2020",
            "ED": "18/01/2020"
          },
          {
            "WN": 4,
            "DT": [
              {
                "MI": 100,
                "VL": "0.00",
                "NA": true,
                "PTG": 0,
                "S": null
              },
              {
                "MI": 101,
                "VL": "0.00",
                "NA": true,
                "PTG": 0,
                "S": null
              },
              {
                "MI": 102,
                "VL": "0.00",
                "NA": true,
                "PTG": 0,
                "S": null
              },
              {
                "MI": 103,
                "VL": "0.00",
                "NA": true,
                "PTG": 0,
                "S": null
              }
            ],
            "SD": "19/01/2020",
            "ED": "25/01/2020"
          }
        ],
        "SD": "01/01/2020",
        "ED": "31/01/2020"
      }
    ],
    "MND": "2019-12-29T00:00:00Z",
    "MXD": "2020-01-25T00:00:00Z"
  }
}
'''

更多信息: