Jmeter: using a Beanshell Assertion to test a null JSON value

≡放荡痞女 提交于 2020-01-14 04:00:13

问题


SI'm trying to test a JSON value that may be = null.

First I tried using jp@gc - JSON Path Assertion. but that doesn't appear to return anything or define the variable if it encounters a null value.

So I was trying to get it to work like this:

  • use jp@gc - JSON Path Extractor to try to extract the value to a variable.
  • use Beanshell Assertion to test to see if the variable exists or has a null value
  • If the variable does not exist, or exists and has a null value, I know that the JSON value was null.

I haven't written any scripts with Jmeter before, so I may be doing something obviously wrong, but here is what I've tried in the Beanshell Assertion:

try {
  String myValue = vars.get("myValue");
  log.info("myValue =" + myValue);
}
catch (e) {
  log.info( "caught exception: "+e );
  String myValue = null;
}


if (myValue.length() > 0  && myValue != 0 ){
 Failure = true;
 FailureMessage = "myValue was " + myValue + ".";
}
else{
  Failure = false;
}

For this test, a null value actually passes the test.

The problem I'm running into is that the try/catch block doesn't work. Instead, I see the following message in the log:

jmeter.assertions.BeanShellAssertion: org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval    Sourced file: inline evaluation of: ``import org.apache.jmeter.util.JMeterUtils; try {   //myValue = JMeterUtils.get . . . '' : Attempt to resolve method: length() on undefined variable or class name: myValue

Can anyone tell me what I'm doing wrong and/or how to get this test of a null JSON value to work?


回答1:


You'll need to amend your code.

  1. Beanshell is not very Java, all clauses will be processed unless you explicitly specify it.
  2. Your myValue string isn't defined, you need to make it more "global"

Correct code will look as simple as follows:

    String myvalue = vars.get("json");

    if (myvalue == null) {
        Failure = false;
    } else if (myvalue.length() > 0) {
        Failure = true;
        FailureMessage = "Expected \"myvalue\" to be null, Actual: " + myvalue;
    }



回答2:


There are many wrong things in this code;

  1. The error message says that it does not recognize myValue. That's because it is defined within the try-catch block. To fix, you have to declare String myValue before the try-catch block. But...
  2. Turns out, you don't need the try-catch block after all, since vars won't throw any Exception but will return null when the given key does not exist.
  3. To check if myValue is not null, use myValue != null.

Here's the updated code:

String myValue = vars.get("myValue");
if(myValue != null){
  log.info("myValue =" + myValue);
  Failure = true;
  FailureMessage = "myValue was " + myValue + ".";
}else{
  //log something here?
  Failure = false;
}



回答3:


http://jmeter-plugins.org/wiki/JSONPathAssertion/

Since v.1.2.1 of jp@gc - JSON Path Assertion it is possible to validate against null value. It's not stable version at the moment of writing this comment, though. It can be downloaded from here: http://jmeter-plugins.org/downloads/all/#Developer-Snapshots



来源:https://stackoverflow.com/questions/20713272/jmeter-using-a-beanshell-assertion-to-test-a-null-json-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!