java - JMeterContext 中的 JMeter setTestLogicalAction 给出 "Cannot reach instance method"

标签 java groovy jmeter

JMeter 5.1。以下链接给出了工作代码 Restarting a user thread conditionally in JMeter其中 setTestLogicalAction 是从 SampleResult 调用的。该方法确实列在 https://jmeter.apache.org/api/org/apache/jmeter/samplers/SampleResult.html 中的方法中。 但是,它也列在https://jmeter.apache.org/api/org/apache/jmeter/threads/JMeterContext.html中。但使用 org.apache.jmeter.threads.JMeterContext.setTestLogicalAction 给出

ERROR o.a.j.u.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: ``    import org.apache.jmeter.samplers.SampleResult;     import org.apache.jmeter . . . '' : Cannot reach instance method: setTestLogicalAction( org.apache.jmeter.threads.JMeterContext$TestLogicalAction ) from static context: org.apache.jmeter.threads.JMeterContext

为什么我不能使用 JMeterContext 中的 setTestLogicalAction?我对 Java 中的类怀念什么?

此外,上面提到的帖子中的代码可以在 beanshell 中运行,但是无论是上面的代码还是修改后的代码都不能在 JSR223+Groovy 中运行。注意到方法不是静态的后,在groovy中的代码如下(两条路径都尝试过,最后我将它们列在一起),没有错误,但线程继续,没有重新启动...

jmco = new org.apache.jmeter.threads.JMeterContext();
jmsr = new org.apache.jmeter.samplers.SampleResult();

jmco.setTestLogicalAction(org.apache.jmeter.threads.JMeterContext.TestLogicalAction.START_NEXT_ITERATION_OF_THREAD);                                 
jmsr.setTestLogicalAction(org.apache.jmeter.threads.JMeterContext.TestLogicalAction.START_NEXT_ITERATION_OF_THREAD); 

我可以在 groovy 脚本中使用 setTestLogicalAction 吗?

附注有趣的是,beanshell 如何调用非静态方法而不需要实例化......

2019/04/01 添加:

def mycontext = org.apache.jmeter.threads.JMeterContextService.getContext()
mycontext.setTestLogicalAction(org.apache.jmeter.threads.JMeterContext.TestLogicalAction.START_NEXT_ITERATION_OF_THREAD);

根据 Dmitri 答案的建议,代码更改为上述内容,但代码不会重新启动线程。

最佳答案

  1. 如果您不太擅长编程,请选择 Flow Control Action采样器而不是尝试在代码中复制其功能,无论如何它会更快。
  2. 无需实例化 JMeterContext 和 SampleResult,因为它们是 pre-defined in JSR223 Test Elements ,而且你做错了,即访问 JMeterContext 的正确方法是调用 JMeterContextService.getContext() function

    enter image description here

  3. Since JMeter 3.1 you should be using JSR223 Test Elements and Groovy language for scripting所以我建议忘记 Beanshell。

关于java - JMeterContext 中的 JMeter setTestLogicalAction 给出 "Cannot reach instance method",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55416643/

相关文章:

java - 在更新查询中添加几列

grails - 将 Java 类注入(inject) Grails Groovy 服务

testing - 正则表达式提取器JMeter的使用?

java - 如何打印另一个类的返回语句?

java - 从多重集中删除集合的出现

java - 在 Java 中使用 Groovy 向类添加方法

java - groovy 脚本中使用 java 的 Hello World 示例给出了空指针

testing - 如何在 Webdriver Sampler 中的 Jmeter 中使用 java 加载 excel 或 csv 文件

jmeter - 如何将用户定义变量的值放入Bean shell Sampler变量中 - Jmeter

java - 当我尝试从 getter 获取数据时,为什么会收到 NullPointerException?