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

时间:2020-01-27 11:19:15

标签: json groovy jmeter jsr223

IAm通过Jmeter测试我的静态API。我得到的Json多次包含相同的许多键,例如(“ id”,MI,DS等)。我无法通过以下代码进行验证:

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

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

响应对象:

{
    "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"
    }
}

IAm出现以下错误:

 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,
                        ^

1 个答案:

答案 0 :(得分:0)

我会按照完整的JSON响应原样编写

这是您的问题,您可能需要使用单引号,例如:

Visible

或使用反斜杠将所有双引号引起来,例如:

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

或为了获得更好的可读性,请使用triple-single-quoted-string,例如:

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

更多信息: