Search code examples
jmeterbeanshell

JMeter BeanShell PostProcessor Attempts to treat a String as byte[]


I have a BeanShell PostProcessor which starts with:

result = ctx.getPreviousResult();
String data = result.getResponseDataAsString();

On the second loop of a LoopController, it will give this in the log:

ERROR - jmeter.util.BeanShellInterpreter: Error invoking bsh method: set    Variable assignment: data: Can't assign byte [] to java.lang.String 
WARN  - jmeter.extractor.BeanShellPostProcessor: Problem in BeanShell script org.apache.jorphan.util.JMeterException: Error invoking bsh method: set    Variable assignment: data: Can't assign byte [] to java.lang.String 

However, if I remove the String declaration, I do not get the warning (so using this to work around) ... i.e. be ambiguous about the type):

    data = result.getResponseDataAsString();

If I print result.getResponseDataAsString().getClass().getName() prior to this line, I get java.lang.String.

Fairly new to BeanShell use in JMeter, but pretty confident that line's the issue.

At the same time, if I have log.info statements prior to the String data = ... assignment, they will not print on the second loop iteration -- which makes me question that assessment.

How do I understand / correct the attempted byte[] assignment?

For reference: here is my test plan:

enter image description here

Basically, each sampler uses result.getResponseDataAsString() to get the response body, does some manipulation to "normalize" it and then sets a value via vars.put(). The BeanShell Assertion uses vars.get() to retrieve the two values, compares them and the sets FailureMessage and Failure accordingly.

Note: both PostProcessor's have the same issue.

Note 2: String data = new String( result.getResponseData() ); is equally problematic (i.e. same error) ... not that I would ever advocate for creating a String from a byte[] without being explicit about encoding / charset.


Solution

  • I created a small .jmx and successfully recreated the issue.

    I tracked down to a specific line in JMeter Code (JMeter 3.0, org.apache.jmeter.extractor.BeanShellPostProcessor:63:

    bshInterpreter.set("data", prev.getResponseData());//$NON-NLS-1$
    

    That tipped me off ... I have a var named data in the above post-processor.

    Looked again @ JMeter UI and ... sure enough, data is a pre-defined script var, typed (per above line) as a byte[].

    I'll assume that BeanShell managed the byte[] <--> String casting when my var wasn't declared with String. With the strict String declaration, type confusion was created.

    Solved by renaming my script var to:

    String localResponseData = result.getResponseDataAsString();
    

    Lesson learned: don't name Bean Shell variables as data (or any other pre-defined one)!