在 Jmeter 脚本中,我使用 JSON 提取器在 2 个变量中收集了一些数据,并在两个 JSON 提取器中将匹配号设置为 -1。现在我需要使用 JSR223 断言检查 API 中第二个变量是否包含第一个变量的数据。我使用的代码是:
// Get the value of dependentFeatID variable from JMeter variables
String dependentFeatID = vars.get("dependentFeatID")
// Check if dependentFeatID is not null
if (dependentFeatID != null) {
// Split the dependentFeatID into an array of IDs
String[] dependentFeatIDs = dependentFeatID.split(",")
// Check if dependentFeatIDs array is not empty
if (dependentFeatIDs.length > 0) {
// Get the first ID from dependentFeatIDs array
String searchString = dependentFeatIDs[0]
log.info("searchString: $searchString")
log.info(dependentFeatIDs[1]+"AND "+dependentFeatIDs[2])
// Get the value of featureIDs variable from JMeter variables
String featureIDs = vars.get("featureIDs")
// Check if featureIDs is not null
if (featureIDs != null) {
// Split the featureIDs into an array of IDs
String[] ids = featureIDs.split(",")
log.info(ids[0]+"AND "+ids[1]+"AND "+ids[2]+"AND "+ids[3])
boolean idFound = false
// Iterate over each ID in the array
for (String id : ids) {
// Trim any leading or trailing whitespace from the ID
id = id.trim()
// Check if the current ID matches the search string
if (id.equals(searchString)) {
// If the ID is found, set idFound to true and break out of the loop
idFound = true
break
}
}
// Set the assertion result based on whether the ID is found
AssertionResult.setFailure(!idFound)
if (!idFound) {
AssertionResult.setFailureMessage("ID not found in var_id: $searchString")
}
} else {
// Log an error if featureIDs is null
log.error("featureIDs variable is null")
AssertionResult.setFailure(true)
AssertionResult.setFailureMessage("featureIDs variable is null")
}
} else {
// Log an error if dependentFeatIDs array is empty
log.error("dependentFeatIDs array is empty")
AssertionResult.setFailure(true)
AssertionResult.setFailureMessage("dependentFeatIDs array is empty")
}
} else {
// Log an error if dependentFeatID is null
log.error("dependentFeatID variable is null")
AssertionResult.setFailure(true)
AssertionResult.setFailureMessage("dependentFeatID variable is null")
}
但是我在执行时遇到方法调用中的空指针错误。当我用来提取数据的 JSON 路径经过测试时,我也获取了这两个变量的值。请在这里帮助我。这是 jmeter 控制台的详细日志:
org.apache.jorphan.util.JMeterException:调用 bsh 方法时出错: eval 源文件:内联评估:
String dependentFeatID = ""; dependentFeatID = vars.get("dependentFeatID"); 英石 。 。 。 '' :类型化变量声明位于 bsh.UtilTargetError.toEvalError(UtilTargetError.java:64) 〜[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19] 在 bsh.UtilEvalError.toEvalError(UtilEvalError.java:84) 〜[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19] 在 bsh.BSHMethodInitation.eval(BSHMethodInitation.java:93) 〜[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19] 在 bsh.BSHPrimaryExpression.eval(BSHPrimaryExpression.java:96) 〜[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19] 在 bsh.BSHPrimaryExpression.eval(BSHPrimaryExpression.java:41) 〜[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19] 在 bsh.BSHVariableDeclarator.eval(BSHVariableDeclarator.java:80) 〜[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19] 在 bsh.BSHTypedVariableDeclaration.eval(BSHTypedVariableDeclaration.java:78) 〜[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19] 在 bsh.Interpreter.eval(Interpreter.java:659) ~[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19] 在 bsh.Interpreter.eval(Interpreter.java:750) 〜[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19] 在 bsh.Interpreter.eval(Interpreter.java:739) ~[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]于 java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(本机 方法)~[?:?] 在 java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 〜[?:?] 在 java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?] 在 java.base/java.lang.reflect.Method.invoke(Method.java:566) 〜[?:?] 在 org.apache.jmeter.util.BeanShellInterpreter.bshInvoke(BeanShellInterpreter.java:162) ~[ApacheJMeter_core.jar:5.6.3] ... 10 个以上 造成原因: java.lang.NullPointerException:方法调用中的空指针 bsh.Name.invokeMethod(Name.java:838) ~[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19] 在 bsh.BSHMethodInitation.eval(BSHMethodInitation.java:69) 〜[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19] 在 bsh.BSHPrimaryExpression.eval(BSHPrimaryExpression.java:96) 〜[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19] 在 bsh.BSHPrimaryExpression.eval(BSHPrimaryExpression.java:41) 〜[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19] 在 bsh.BSHVariableDeclarator.eval(BSHVariableDeclarator.java:80) 〜[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19] 在 bsh.BSHTypedVariableDeclaration.eval(BSHTypedVariableDeclaration.java:78) 〜[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19] 在 bsh.Interpreter.eval(Interpreter.java:659) ~[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19] 在 bsh.Interpreter.eval(Interpreter.java:750) 〜[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19] 在 bsh.Interpreter.eval(Interpreter.java:739) ~[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]于 java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(本机 方法)~[?:?] 在 java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 〜[?:?] 在 java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?] 在 java.base/java.lang.reflect.Method.invoke(Method.java:566) 〜[?:?] 在 org.apache.jmeter.util.BeanShellInterpreter.bshInvoke(BeanShellInterpreter.java:162) ~[ApacheJMeter_core.jar:5.6.3] ... 10 个以上String dependentFeatID = ""; dependentFeatID = vars.get("dependentFeatID"); St . . . '' : Typed variable declaration at org.apache.jmeter.util.BeanShellInterpreter.bshInvoke(BeanShellInterpreter.java:179) ~[ApacheJMeter_core.jar:5.6.3] at org.apache.jmeter.util.BeanShellInterpreter.eval(BeanShellInterpreter.java:185) ~[ApacheJMeter_core.jar:5.6.3] at org.apache.jmeter.util.BeanShellTestElement.processFileOrScript(BeanShellTestElement.java:174) ~[ApacheJMeter_core.jar:5.6.3] at org.apache.jmeter.util.BeanShellTestElement.processFileOrScript(BeanShellTestElement.java:140) ~[ApacheJMeter_core.jar:5.6.3] at org.apache.jmeter.assertions.BeanShellAssertion.getResult(BeanShellAssertion.java:106) [ApacheJMeter_components.jar:5.6.3] at org.apache.jmeter.threads.JMeterThread.processAssertion(JMeterThread.java:949) [ApacheJMeter_core.jar:5.6.3] at org.apache.jmeter.threads.JMeterThread.checkAssertions(JMeterThread.java:918) [ApacheJMeter_core.jar:5.6.3] at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:586) [ApacheJMeter_core.jar:5.6.3] at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:501) [ApacheJMeter_core.jar:5.6.3] at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:268) [ApacheJMeter_core.jar:5.6.3] at java.base/java.lang.Thread.run(Thread.java:834) [?:?] Caused by: java.lang.reflect.InvocationTargetException at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:?] at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?] at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[?:?] at org.apache.jmeter.util.BeanShellInterpreter.bshInvoke(BeanShellInterpreter.java:162) ~[ApacheJMeter_core.jar:5.6.3] ... 10 more Caused by: bsh.TargetError: Sourced file: inline evaluation of:
如果您使用 Beanshell 每个语句必须以分号结尾,即代替:
String dependentFeatID = vars.get("dependentFeatID")
你需要使用
String dependentFeatID = vars.get("dependentFeatID");
等等
但是,更好的想法是切换到 Groovy 语言,这样您就不必添加分号,因为 Groovy 更宽容。此外从 JMeter 3.1 开始建议使用 Groovy 进行脚本编写
有关 JMeter 中 Groovy 脚本编写的更多信息:Apache Groovy:Groovy 的用途是什么?