Search code examples
jmeterjmeter-pluginsjmeter-maven-plugin

JMeter Beanshell problem with imports at top of script: Error invoking bsh method: eval


I am getting a error in a Beanshell Postprocessor, after a HTTP Request returning a JSON array :

My JMeter is launched using mvn jmeter:configure jmeter:gui using Maven plugin configured like so:

    <plugin>
        <groupId>com.lazerycode.jmeter</groupId>
        <artifactId>jmeter-maven-plugin</artifactId>
        <version>3.5.0</version>
        <executions>
            ....
        </executions>
        <configuration>
            <generateReports>true</generateReports>
            <testFilesIncluded>
               ....
            </testFilesIncluded>
            <jmeterVersion>5.4.3</jmeterVersion>
            <ignoreResultFailures>true</ignoreResultFailures>
            <testPlanLibraries>
                <artifact>com.googlecode.json-simple:json-simple:1.1.1</artifact>
                <artifact>com.fasterxml.jackson.core:jackson-databind:2.14.1</artifact>
                <artifact>com.github.javafaker:javafaker:1.0.2</artifact>
            </testPlanLibraries>
            <jmeterExtensions>
                <artifact>kg.apc:jmeter-plugins-manager:1.6</artifact>
                <artifact>kg.apc:jmeter-plugins-standard:1.4.0</artifact>
                <artifact>kg.apc:jmeter-plugins-common:1.4.0</artifact>
                <artifact>kg.apc:jmeter-plugins-extras:1.4.0</artifact>
                <artifact>kg.apc:jmeter-plugins-extras-libs:1.4.0</artifact>
                <artifact>com.blazemeter:jmeter-parallel:0.11</artifact>
            </jmeterExtensions>
            .....
        .....

And my script looks like this:

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
JSONParser jsonParser = new JSONParser();
JSONObject jsonObject;
JSONObject jsonArray;
try {
    jsonObject = (JSONObject)jsonParser.parse(prev.getResponseDataAsString());
    jsonArray = (JSONArray)jsonObject.get("value");

//    List<JSONObject> jsonList = new ArrayList();
//    if (jsonArray != null) {
//        for (Object object : jsonArray) {
//            JSONObject jsonObj = (JSONObject)object;
//            jsonList.add(jsonObj);
//        }
//    }

} catch (Exception e) {
    e.printStackTrace();
}
log.info("jsonArray: " + jsonArray);

So, hoping someone might be able to tell me what I am doing wrong?

Error invoking bsh method: eval Sourced file: inline evaluation of: `` import org.json.simple.JSONArray;  
   import org.json.simple.JSONObject;  import o . . . '' : Variable assignment: 
   jsonArray: Can't assign org.json.simple.JSONArray to org.json.simple.JSONObject
Problem in BeanShell script: org.apache.jorphan.util.JMeterException: Error 
   invoking bsh method: eval    Sourced file: inline evaluation of: 
   `` import org.json.simple.JSONArray;  import org.json.simple.JSONObject;  import o . . . '' : Variable assignment: jsonArray: Can't assign org.json.simple.JSONArray to org.json.simple.JSONObject

I see nothing wrong with this code. Please help.

This is the problematic line, that throws the error:

jsonArray = (JSONArray)jsonObject.get("value");

Solution

  • I see nothing wrong with the code as well. It seems that the value attribute of your response is not a JSON Array hence your cast fails and you're getting an error. We need to

    • see the response body
    • know what you're trying to achieve

    in order to suggest the best possible option


    There is something wrong with the overall approach:

    1. JMeter already comes with Json-smart library which provides possibility to parse JSON, adding another library is not really needed
    2. Since JMeter 3.1 it's recommended to use JSR223 Test Elements and Groovy language for scripting mainly because Groovy provides much better performance comparing to other scripting options
    3. Groovy has built-in JSON support so no 3rd-party libraries are required
    4. JMeter provides JSON Extractor and JSON JMESPath Extractor which can cover 99% of needs when it comes to correlation.