JSONAssert.assertEquals

I keep getting an error when running the following code. Any idea about what could be happening?

import org.skyscreamer.jsonassert.JSONAssert
import org.skyscreamer.jsonassert.JSONCompareMode

String actual = "{id:123, name:\"John\"}"; 
JSONAssert.assertEquals("{id:123,name:\"John\"}", actual, JSONCompareMode.LENIENT);

Error:

Reason:

org.codehaus.groovy.runtime.InvokerInvocationException: java.lang.NoClassDefFoundError: org/json/JSONException

at com.ws.Compare.invokeMethod(Compare.groovy)

at com.kms.katalon.core.main.CustomKeywordDelegatingMetaClass.invokeStaticMethod(CustomKeywordDelegatingMetaClass.java:49)

at location_compare.run(location_compare:18)

at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)

at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)

at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:328)

at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:319)

at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:298)

at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:290)

at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:224)

at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:106)

at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:97)

at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)

at TempTestCase1546712791045.run(TempTestCase1546712791045.groovy:22)

Caused by: java.lang.NoClassDefFoundError: org/json/JSONException

at com.ws.Compare.compareWsResponses(Compare.groovy:44)

at com.ws.Compare.invokeMethod(Compare.groovy)

at com.kms.katalon.core.main.CustomKeywordDelegatingMetaClass.invokeStaticMethod(CustomKeywordDelegatingMetaClass.java:49)

at Script1546631654104.run(Script1546631654104.groovy:18)

... 11 more

For reference:

I suppose you need to add org.json library as well: http://central.maven.org/maven2/org/json/json/20180813/json-20180813.jar , source: https://github.com/stleary/JSON-java

1 Like

Thanks @Zarashima!

That worked for my simple example:

//this worked fine and pass/failed as expected
String actual = “{id:123, name:“John”}”;
JSONAssert.assertEquals("{id:123,name:“John”}", actual, JSONCompareMode.LENIENT);

but not when I tried it with my actual WS response. I tried:

//parseText
def parsedJson1 = new groovy.json.JsonSlurper().parseText(response1.getResponseBodyContent())
def parsedJson2 = new groovy.json.JsonSlurper().parseText(response2.getResponseBodyContent())

//these did not work (errors below)

  1. JSONAssert.assertEquals(response1, response1, JSONCompareMode.LENIENT)
  2. JSONAssert.assertEquals(parsedJson1, parsedJson2, JSONCompareMode.LENIENT)
  3. JSONAssert.assertEquals(parsedJson1.toString(), parsedJson2.toString(), JSONCompareMode.LENIENT)

Error for each:

1

Possible solutions: assertEquals(java.lang.String, java.lang.String, org.skyscreamer.jsonassert.JSONCompareMode), assertEquals(java.lang.String, org.json.JSONArray, org.skyscreamer.jsonassert.JSONCompareMode), assertEquals(java.lang.String, org.json.JSONObject, org.skyscreamer.jsonassert.JSONCompareMode), assertEquals(org.json.JSONArray, org.json.JSONArray, org.skyscreamer.jsonassert.JSONCompareMode), assertEquals(org.json.JSONObject, org.json.JSONObject, org.skyscreamer.jsonassert.JSONCompareMode), assertEquals(java.lang.String, java.lang.String, boolean)

at com.ws.Compare.invokeMethod(Compare.groovy)

at com.kms.katalon.core.main.CustomKeywordDelegatingMetaClass.invokeStaticMethod(CustomKeywordDelegatingMetaClass.java:49)

at location_compare.run(location_compare:18)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:328)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:319)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:298)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:290)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:224)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:106)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:97)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at TempTestCase1546810597945.run(TempTestCase1546810597945.groovy:22)
Caused by: groovy.lang.MissingMethodException: No signature of method: static org.skyscreamer.jsonassert.JSONAssert.assertEquals() is applicable for argument types: (com.kms.katalon.core.testobject.ResponseObject, com.kms.katalon.core.testobject.ResponseObject, org.skyscreamer.jsonassert.JSONCompareMode) values: [200 86 KB, 200 86 KB, LENIENT]

Possible solutions: assertEquals(java.lang.String, java.lang.String, org.skyscreamer.jsonassert.JSONCompareMode), assertEquals(java.lang.String, org.json.JSONArray, org.skyscreamer.jsonassert.JSONCompareMode), assertEquals(java.lang.String, org.json.JSONObject, org.skyscreamer.jsonassert.JSONCompareMode), assertEquals(org.json.JSONArray, org.json.JSONArray, org.skyscreamer.jsonassert.JSONCompareMode), assertEquals(org.json.JSONObject, org.json.JSONObject, org.skyscreamer.jsonassert.JSONCompareMode), assertEquals(java.lang.String, java.lang.String, boolean)

at com.ws.Compare.compareWsResponses(Compare.groovy:46)
at com.ws.Compare.invokeMethod(Compare.groovy)
at com.kms.katalon.core.main.CustomKeywordDelegatingMetaClass.invokeStaticMethod(CustomKeywordDelegatingMetaClass.java:49)

2

org.codehaus.groovy.runtime.InvokerInvocationException: groovy.lang.MissingMethodException: No signature of method: static org.skyscreamer.jsonassert.JSONAssert.assertEquals() is applicable for argument types: (java.util.ArrayList, java.util.ArrayList, org.skyscreamer.jsonassert.JSONCompareMode) values: [[[AdministrativeArea:[CountryID:BD, EnglishName:Dhaka, EnglishType:Division, …], …], …], …]

Possible solutions: assertEquals(java.lang.String, java.lang.String, org.skyscreamer.jsonassert.JSONCompareMode), assertEquals(java.lang.String, org.json.JSONArray, org.skyscreamer.jsonassert.JSONCompareMode), assertEquals(java.lang.String, org.json.JSONObject, org.skyscreamer.jsonassert.JSONCompareMode), assertEquals(org.json.JSONArray, org.json.JSONArray, org.skyscreamer.jsonassert.JSONCompareMode), assertEquals(org.json.JSONObject, org.json.JSONObject, org.skyscreamer.jsonassert.JSONCompareMode), assertEquals(java.lang.String, java.lang.String, boolean)

at com.ws.Compare.invokeMethod(Compare.groovy)

at com.kms.katalon.core.main.CustomKeywordDelegatingMetaClass.invokeStaticMethod(CustomKeywordDelegatingMetaClass.java:49)

at location_compare.run(location_compare:18)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:328)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:319)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:298)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:290)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:224)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:106)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:97)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at TempTestCase1546810990914.run(TempTestCase1546810990914.groovy:22)

Caused by: groovy.lang.MissingMethodException: No signature of method: static org.skyscreamer.jsonassert.JSONAssert.assertEquals() is applicable for argument types: (java.util.ArrayList, java.util.ArrayList, org.skyscreamer.jsonassert.JSONCompareMode) values: [[[AdministrativeArea:[CountryID:BD, EnglishName:Dhaka, EnglishType:Division, …], …], …], …]

Possible solutions: assertEquals(java.lang.String, java.lang.String, org.skyscreamer.jsonassert.JSONCompareMode), assertEquals(java.lang.String, org.json.JSONArray, org.skyscreamer.jsonassert.JSONCompareMode), assertEquals(java.lang.String, org.json.JSONObject, org.skyscreamer.jsonassert.JSONCompareMode), assertEquals(org.json.JSONArray, org.json.JSONArray, org.skyscreamer.jsonassert.JSONCompareMode), assertEquals(org.json.JSONObject, org.json.JSONObject, org.skyscreamer.jsonassert.JSONCompareMode), assertEquals(java.lang.String, java.lang.String, boolean)

at com.ws.Compare.compareWsResponses(Compare.groovy:46)
at com.ws.Compare.invokeMethod(Compare.groovy)
at com.kms.katalon.core.main.CustomKeywordDelegatingMetaClass.invokeStaticMethod(CustomKeywordDelegatingMetaClass.java:49)
at Script1546631654104.run(Script1546631654104.groovy:18)

3

Test Cases/LocationAPI/Compare/location_compare FAILED.

Reason:

org.json.JSONException: Expected a ‘,’ or ‘]’ at 21 [character 22 line 1]

at org.json.JSONTokener.syntaxError(JSONTokener.java:505)
at org.json.JSONArray.<init>(JSONArray.java:145)
at org.json.JSONTokener.nextValue(JSONTokener.java:434)
at org.json.JSONArray.<init>(JSONArray.java:125)
at org.json.JSONArray.<init>(JSONArray.java:162)
at org.skyscreamer.jsonassert.JSONParser.parseJSON(JSONParser.java:46)
at org.skyscreamer.jsonassert.JSONCompare.compareJSON(JSONCompare.java:49)
at org.skyscreamer.jsonassert.JSONCompare.compareJSON(JSONCompare.java:125)
at org.skyscreamer.jsonassert.JSONAssert.assertEquals(JSONAssert.java:415)
at org.skyscreamer.jsonassert.JSONAssert.assertEquals(JSONAssert.java:394)
at org.skyscreamer.jsonassert.JSONAssert$assertEquals.call(Unknown Source)
at com.ws.Compare.compareWsResponses(Compare.groovy:48)
at com.ws.Compare.invokeMethod(Compare.groovy)

at com.kms.katalon.core.main.CustomKeywordDelegatingMetaClass.invokeStaticMethod(CustomKeywordDelegatingMetaClass.java:49)

at location_compare.run(location_compare:18)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:328)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:319)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:298)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:290)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:224)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:106)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:97)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at TempTestCase1546813775140.run(TempTestCase1546813775140.groovy:22)

1 and 2 are understandable.

  • ‘response’ is of type ResponseObject
  • JsonSlurper().parseText(response1.getResponseBodyContent()) will build a map

So, none of them will match with an expected signature (String, JSONObject, JSONArray)

For 3, kindly post also the actual json strings, looks like in fact it worked, but raised an exception because of a malformed Json (reason: map > to string I guess)

try this:
JSONAssert.assertEquals(response1.getResponseBodyContent(), response2.getResponseBodyContent(), JSONCompareMode.LENIENT)

or this
JSONAssert.assertEquals(response1.getResponseText(), response2.getResponseText(), JSONCompareMode.LENIENT)

ref: https://api-docs.katalon.com/studio/v4.6.0.2/api/com/kms/katalon/core/testobject/ResponseObject.html#getResponseBodyContent()

@Ibus Thanks!

both of those worked!

The only issue I have is that it only ‘reports’ if it fails. I would like to see the details of the comparison if that is possible?

Matt

@Matt_Thurman

I didn’t used this library, but if it throws an exception in case of failure, you can use a try - catch and grab the error message … if is returning just a boolean I have no idea.

LE:other option is, without this library, simply parse the Json to map, as you did before, and just compare the maps with the groovy’s assert, e.g:

def parsedJson1 = new groovy.json.JsonSlurper().parseText(response1.getResponseBodyContent())
def parsedJson2 = new groovy.json.JsonSlurper().parseText(response2.getResponseBodyContent())
assert parsedJson1 == parsedJson2

Thanks @Ibus !