Problem in JSR223 script, JSR223 PostProcessor : javax.script.ScriptException

9.4k Views Asked by At

I am using Jmeter 5.0 where i have piece of java code written inside a JSR223 PostProcessor. The code is as follows -

import java.util.Map;
import java.util.HashMap;


Map gamePlayHistoryMap = new HashMap();
gamePlayHistoryMap.put(${playerId}, ${GameplayHistoryId});
props.put("GamePlayHistoryMap", gamePlayHistoryMap);

Map payLevelDetailsMap = new HashMap();
payLevelDetailsMap.put(${playerId}, ${PayLevelDetails});
props.put("PayLevelDetailsMap", payLevelDetailsMap);

However when i execute the test plan, in the console i get the following error -

javax.script.ScriptException: In file: inline evaluation of: import java.util.Map; import java.util.HashMap; Map gamePlayHistoryMap = new H . . . '' Encountered "( 107 , )" at line 6, column 23. in inline evaluation of:import java.util.Map; import java.util.HashMap; Map gamePlayHistoryMap = new H . . . '' at line number 6

Can someone help me in pointing where i might have gone wrong ?

3

There are 3 best solutions below

6
Ori Marko On BEST ANSWER

Don't use ${} in JSR223 scripts, use instead vars.get("") to get varibles

gamePlayHistoryMap.put(vars.get("playerId"), vars.get("GameplayHistoryId"));

It seems that GameplayHistoryId is empty, in such case add default value in JSONExtractor or fail test

See JMeter's best practices for JSR223 scripting:

In this case, ensure the script does not use any variable using ${varName} as caching would take only first value of ${varName}. Instead use : vars.get("varName")

0
Dmitri T On
  1. Since JMeter 3.1 you should be using groovy language for scripting, looking into your exception details it appears you're using java which is not real Java, it's Beanshell interpreter which has worse performance comparing to Groovy and you have to stick to Java 5 syntax.
  2. Don't inline JMeter Functions and/or Variables into scripts as they might be resolved into something causing script failures and in case of Groovy they conflict with GString templates and compilation caching feature. Use vars shorthand for JMeterVariables class to read existing variables values and create new ones, i.e. replace this line:

    gamePlayHistoryMap.put(${playerId}, ${GameplayHistoryId});
    

    with this one:

    gamePlayHistoryMap.put(vars.get('playerId'), vars.get('GameplayHistoryId'));
    
0
Leon Proskurov On

You are missing the Map key/value definition.

Map <String, String> gamePlayHistoryMap = new HashMap<>();
gamePlayHistoryMap.put(${playerId}, ${GameplayHistoryId});

Not sure about the answer of:

Don't use ${} in JSR223 scripts, use instead vars.get("")

not sure it has anything to do with it.