简体   繁体   中英

Do I need to import any Jar file in Jmeter project in order to use " import groovy.json.*"?? how should I do that?

Iam trying to validate the JSON of my API through following code but its failing. What could be the reason ?

import groovy.json.*

JSONObject obj1 = '''{"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":"20.00","NA":false,"PTG":0.0,"S":null},{"MI":101,"VL":"0.20","NA":false,"PTG":0.0,"S":null},{"MI":102,"VL":"0.00","NA":true,"PTG":0.0,"S":null},{"MI":103,"VL":"0.20","NA":false,"PTG":0.0,"S":null}],"MR":[{"MI":100,"VL":"1,536.00","NA":false,"PTG":1.0,"S":"danger"},{"MI":101,"VL":"0.00","NA":true,"PTG":0.0,"S":null},{"MI":102,"VL":"0.00","NA":true,"PTG":0.0,"S":null},{"MI":103,"VL":"0.00","NA":true,"PTG":0.0,"S":null}],"WR":[{"WN":1,"DT":[{"MI":100,"VL":"0.00","NA":true,"PTG":0.0,"S":null},{"MI":101,"VL":"0.00","NA":true,"PTG":0.0,"S":null},{"MI":102,"VL":"0.00","NA":true,"PTG":0.0,"S":null},{"MI":103,"VL":"0.00","NA":true,"PTG":0.0,"S":null}],"SD":"29/12/2019","ED":"04/01/2020"},{"WN":2,"DT":[{"MI":100,"VL":"1,536.00","NA":false,"PTG":1.0,"S":"danger"},{"MI":101,"VL":"0.00","NA":true,"PTG":0.0,"S":null},{"MI":102,"VL":"0.00","NA":true,"PTG":0.0,"S":null},{"MI":103,"VL":"0.00","NA":true,"PTG":0.0,"S":null}],"SD":"05/01/2020","ED":"11/01/2020"},{"WN":3,"DT":[{"MI":100,"VL":"0.00","NA":true,"PTG":0.0,"S":null},{"MI":101,"VL":"0.00","NA":true,"PTG":0.0,"S":null},{"MI":102,"VL":"0.00","NA":true,"PTG":0.0,"S":null},{"MI":103,"VL":"0.00","NA":true,"PTG":0.0,"S":null}],"SD":"12/01/2020","ED":"18/01/2020"},{"WN":4,"DT":[{"MI":100,"VL":"0.00","NA":true,"PTG":0.0,"S":null},{"MI":101,"VL":"0.00","NA":true,"PTG":0.0,"S":null},{"MI":102,"VL":"0.00","NA":true,"PTG":0.0,"S":null},{"MI":103,"VL":"0.00","NA":true,"PTG":0.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"}}'''
JSONObject obj2 = prev.getResponseDataasString()
ObjectMapper mapper = new ObjectMapper();
JsonNode expected = mapper.readTree(obj1.toString());
JsonNode actual = mapper.readTree(obj2.toString());
if (!expected.equals(actual)) {
    AssertionResult.setFailure(true)
}

ERROR:

Assertion error: true Assertion failure: false Assertion failure message: javax.script.ScriptException: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script36.groovy: 6: unable to resolve class JSONObject @ line 6, column 12. JSONObject obj1 = '''{"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":"20.00","NA":false,"PTG":0.0,"S":null},{"MI":101,"VL":"0.20","NA":false,"PTG":0.0,"S":null},{"MI":102,"VL":"0.00","NA":true,"PTG":0.0,"S":null},{"MI":103,"VL":"0.20","NA":false,"PTG":0.0,"S":null}],"MR":[{"MI":100,"VL":"1,53 6.00","NA":false,"PTG":1.0,"S":"danger"},{"MI":101,"VL":"0.00","NA":true,"PTG":0.0,"S":null},{"MI":102,"VL":"0.00","NA":true,"PTG":0.0,"S":null},{"MI":103,"VL":"0.00","NA":true,"PTG":0.0,"S":null}],"WR":[{"WN":1,"DT":[{"MI":100,"VL":"0.00","NA":true,"PTG":0.0,"S":null},{"MI":101,"VL":"0.00","NA":true,"PTG":0.0,"S":null},{"MI":102,"VL":"0.00","NA":true,"PTG":0.0,"S":null},{"MI":103,"VL":"0.00","NA":true,"PTG":0.0,"S":null}],"SD":"29/12/2019","ED":"04/01/2020"},{"WN":2,"DT":[{"MI":100,"VL":"1,536.00","NA":false,"PTG":1.0,"S":"danger"},{"MI":101,"VL":"0.00","NA":true,"PTG":0.0,"S":null},{"MI":102,"VL":"0.00","NA":true,"PTG":0.0,"S":null},{"MI":103,"VL":"0.00","NA":true,"PTG":0.0,"S":null}],"SD":"05/01/2020","ED":"11/01/2020"},{"WN":3,"DT":[{"MI":100,"VL":"0.00","NA":true,"PTG":0.0,"S":null},{"MI":101,"VL":"0.00","NA":true,"PTG":0.0,"S":null},{"MI":102,"VL":"0.00","NA":true,"PTG":0.0,"S":null},{"MI":103,"VL":"0.00","NA":true,"PTG":0.0,"S":null}],"SD":"12/01/2020","ED":"18/01/2020"},{"WN":4 ,"DT":[{"MI":100,"VL":"0.00","NA":true,"PTG":0.0,"S":null},{"MI":101,"VL":"0.00","NA":true,"PTG":0.0,"S":null},{"MI":102,"VL":"0.00","NA":true,"PTG":0.0,"S":null},{"MI":103,"VL":"0.00","NA":true,"PTG":0.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"}}''' ^

Script36.groovy: 7: unable to resolve class JSONObject @ line 7, column 12. JSONObject obj2 = prev.getResponseDataasString() ^

Script36.groovy: 8: unable to resolve class ObjectMapper @ line 8, column 14. ObjectMapper mapper = new ObjectMapper(); ^

Script36.groovy: 8: unable to resolve class ObjectMapper @ line 8, column 23. ObjectMapper mapper = new ObjectMapper(); ^

Script36.groovy: 9: unable to resolve class JsonNode @ line 9, column 10. JsonNode expected = mapper.readTree(obj1.toString()); ^

Script36.groovy: 10: unable to resolve class JsonNode @ line 10, column 10. JsonNode actual = mapper.readTree(obj2.toString()); ^

6 errors

JSONObject is not a groovy object - its probably from some library (like simple json) that should be important if you want to use this class.

However, the presented groovy code is wrong: obj1 is a GString so the casting is impossible here. So you should really the tutorial about the json processing library of your choice for the correct syntax.

All-in-all, as a general note, if you're already using Groovy, consider using built-in JSONSlurper :

import groovy.json.*

def obj1 =...
def slurper = new JsonSlurper()
def json1Obj = slurper.parseText(obj1)
...

Looking into ObjectMapper it seems you're trying to use Jackson , it means that you need jackson-databind with the minimal set of dependencies ( jackson-core and jackson-annotations ) inJMeter Classpath

You don't also need these JSONObject and JsonNode , your code can be amended to:

def obj1 = '''your json here'''
def obj2 = prev.getResponseDataAsString()

def mapper = new com.fasterxml.jackson.databind.ObjectMapper()

def expected = mapper.readTree(obj2)
def actual = mapper.readTree(obj2)

if (!expected.equals(actual)) {
    AssertionResult.setFailure(true)
}

More information:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM