【问题标题】:Jmeter JSR223 Method addOptions( com.jayway.jsonpath.Option ) not found in class'com.jayway.jsonpath.ConfigurationJmeter JSR223方法addOptions(com.jayway.jsonpath.Option)在类'com.jayway.jsonpath.Configuration中找不到
【发布时间】:2021-05-06 19:15:20
【问题描述】:

在 Jmeter JSR223 采样器中运行下面的 java 代码时出错

import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.Option;
import net.minidev.json.JSONArray;


String json = "{\"store\":{\"book\":[{\"category\":\"reference\",\"author\":\"Nigel Rees\",\"title\":\"Sayings of the Century\",\"price\":8.95},{\"category\":\"fiction\",\"author\":\"Evelyn Waugh\",\"title\":\"Sword of Honour\",\"price\":12.99},{\"category\":\"fiction\",\"author\":\"Herman Melville\",\"title\":\"Moby Dick\",\"isbn\":\"0-553-21311-3\",\"price\":8.99},{\"category\":\"fiction\",\"author\":\"J. R. R. Tolkien\",\"title\":\"The Lord of the Rings\",\"isbn\":\"0-395-19395-8\",\"price\":22.99}],\"bicycle\":{\"color\":\"red\",\"price\":19.95}},\"expensive\":10}";

        String jsonPath = "$..book[?(@.author == 'Nigel Rees')].title";

        Configuration config = Configuration.defaultConfiguration().addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL);

        JSONArray authorsArr = JsonPath.using(config).parse(json).read(jsonPath);

        System.out.println(authorsArr.get(0).toString());

json-path-2.4.0.jar 被添加到 ..\lib\ext 下,并假设它在 Jmeter 启动时自动加载。

以上代码在 IDE 中检查,运行良好。

JSR223 错误

2021-02-02 17:19:10,580 ERROR o.a.j.p.j.s.JSR223Sampler: Problem in JSR223 script JSR223 Sampler, message: javax.script.ScriptException: Sourced file: inline evaluation of: ``import com.jayway.jsonpath.Configuration; import com.jayway.jsonpath.JsonPath; i . . . '' : Typed variable declaration : Error in method invocation: Method addOptions( com.jayway.jsonpath.Option ) not found in class'com.jayway.jsonpath.Configuration' : at Line: 11 : in file: inline evaluation of: ``import com.jayway.jsonpath.Configuration; import com.jayway.jsonpath.JsonPath; i . . . '' : .addOptions ( Option .DEFAULT_PATH_LEAF_TO_NULL ) 
 in inline evaluation of: ``import com.jayway.jsonpath.Configuration; import com.jayway.jsonpath.JsonPath; i . . . '' at line number 11
javax.script.ScriptException: Sourced file: inline evaluation of: ``import com.jayway.jsonpath.Configuration; import com.jayway.jsonpath.JsonPath; i . . . '' : Typed variable declaration : Error in method invocation: Method addOptions( com.jayway.jsonpath.Option ) not found in class'com.jayway.jsonpath.Configuration' : at Line: 11 : in file: inline evaluation of: ``import com.jayway.jsonpath.Configuration; import com.jayway.jsonpath.JsonPath; i . . . '' : .addOptions ( Option .DEFAULT_PATH_LEAF_TO_NULL ) 
 in inline evaluation of: ``import com.jayway.jsonpath.Configuration; import com.jayway.jsonpath.JsonPath; i . . . '' at line number 11
    at bsh.engine.BshScriptEngine.evalSource(BshScriptEngine.java:93) ~[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
    at bsh.engine.BshScriptEngine.eval(BshScriptEngine.java:46) ~[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
    at javax.script.AbstractScriptEngine.eval(Unknown Source) ~[?:1.8.0_241]
    at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:223) ~[ApacheJMeter_core.jar:4.0 r1823414]
    at org.apache.jmeter.protocol.java.sampler.JSR223Sampler.sample(JSR223Sampler.java:69) [ApacheJMeter_java.jar:4.0 r1823414]
    at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:490) [ApacheJMeter_core.jar:4.0 r1823414]
    at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:416) [ApacheJMeter_core.jar:4.0 r1823414]
    at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:250) [ApacheJMeter_core.jar:4.0 r1823414]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_241]

【问题讨论】:

    标签: jmeter jsonpath jsr223


    【解决方案1】:
    1. 将“Language”更改为groovy,如果您选择java,它并不是真正的Java,而是Beanshell

      • 不完全符合 Java 标准
      • 与 Groovy 相比性能更差

      所以according to JMeter Best Practices you should switch to Groovy since JMeter 3.1。更多信息:Apache Groovy - Why and How You Should Use It

    2. 改变这一行:

      String jsonPath = "$..book[?(@.author == 'Nigel Rees')].title";
      

      到这个

      String jsonPath = '$..book[?(@.author == \'Nigel Rees\')].title'
      

      因为您的 Java 语法与 Groovy GString Template Engine 略有冲突

    3. 您不应该需要任何额外的 .jar,代码将在 vanilla JMeter 实例上运行

    还要注意,Groovy 有 built-in JSON support

    【讨论】:

      猜你喜欢
      • 2017-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-06
      • 1970-01-01
      相关资源
      最近更新 更多