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

泄露秘密 提交于 2019-12-08 05:10:30

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;
    }
Worakarn Isaratham

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;
}

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

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