Как я могу проверить вложенный ответ JSON, используя groovy в Jmeter? - PullRequest
0 голосов
/ 27 января 2020

IAm тестирование моего успокоительного API через Jmeter. Я получаю Json, который включает в себя одно и то же множество ключей, таких как ("id", MI, DS et c) несколько раз. Я не могу проверить с помощью следующего кода:

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

1 Ответ

0 голосов
/ 27 января 2020

Я пишу после завершения JSON Ответ в том виде, как он есть

это ваша проблема, вы должны использовать одинарные кавычки, такие как:

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

или экранируйте все двойные кавычки с помощью обратной косой черты sh, например:

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

, или для лучшей читаемости используйте тройную одинарную кавычку , например:

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

Дополнительная информация:

...