I have this Groovy script where I pass an empty string to the method 'toJson' (it's in groovy.json.JsonOutput).
I want to test if our API is handling and key with an empty string, so "key": "",
I actually want to test three things:
- Test with valid value ("key": "valid value")
- Test with empty value ("key": "")
- Test without the key and the value (null)
I use the following groovy-code to build a JSON-body. In the actual script there are some statements where the script checks if a value is present, if not it will be excluded.
import groovy.json.*;
class Example {
def exampleKey1
def exampleKey2
def exampleKey3
}
String valueexampleKey1 = "value";
String valueexampleKey2 = "";
String valueexampleKey3 = null;
def generator = new JsonGenerator.Options()
.excludeNulls()
.build()
Example example = new Example (
exampleKey1: valueexampleKey1,
exampleKey2: valueexampleKey2,
exampleKey3: valueexampleKey3
);
String unformattedJSON = generator.toJson(example);
String formattedJSON = JsonOutput.prettyPrint(unformattedJSON);
vars.put("generatedBody", formattedJSON);
The following exception is thrown:
2020-02-12 13:05:26,168 ERROR o.a.j.JMeter: Uncaught exception: java.lang.IllegalAccessError: class groovy.json.DefaultJsonGenerator tried to access private field groovy.json.JsonOutput.EMPTY_STRING_CHARS (groovy.json.DefaultJsonGenerator and groovy.json.JsonOutput are in unnamed module of loader org.apache.jmeter.DynamicClassLoader @1ae369b7) at groovy.json.DefaultJsonGenerator.writeCharSequence(DefaultJsonGenerator.java:256) ~[groovy-json-2.5.7.jar:2.5.7] at groovy.json.DefaultJsonGenerator.writeObject(DefaultJsonGenerator.java:190) ~[groovy-json-2.5.7.jar:2.5.7] at groovy.json.DefaultJsonGenerator.writeMapEntry(DefaultJsonGenerator.java:387) ~[groovy-json-2.5.7.jar:2.5.7] at groovy.json.DefaultJsonGenerator.writeMap(DefaultJsonGenerator.java:375) ~[groovy-json-2.5.7.jar:2.5.7] at groovy.json.DefaultJsonGenerator.writeObject(DefaultJsonGenerator.java:237) ~[groovy-json-2.5.7.jar:2.5.7] at groovy.json.DefaultJsonGenerator.writeObject(DefaultJsonGenerator.java:164) ~[groovy-json-2.5.7.jar:2.5.7] at groovy.json.DefaultJsonGenerator.toJson(DefaultJsonGenerator.java:98) ~[groovy-json-2.5.7.jar:2.5.7] at groovy.json.JsonGenerator$toJson.call(Unknown Source) ~[?:?] at Script27.run(Script27.groovy:111) ~[?:?]
Is there a workaround for this problem?
groovy.json.JsonOutput.toJson("")works in a groovysh; so either you are doing something different or JMeter(?) influences the result. Please provide the code and if this is no generic groovy question also add the tags for this third party. - cfrick