开发者问题收集

如何解决“无法在空对象上调用方法 contains()”

2021-08-13
5857

在我的脚本中,我尝试进行 get 调用并对响应进行断言。对于有效响应,此方法工作正常;但对于无效响应,我尝试获取请求 URL 的 UUID,但不起作用。

请求 URL:

http://localhost:8080/log/log-events/6cef99be-591e-42e4-b07c-34b45f9540df

失败时的响应:

{"trace-id":"8000043e-0001-eb00-b63f-84710c7967bb","invalid-param":[],"type":"https://tools.ietf.org/html/rfc7231#section-6.5.4","title":"The specified resource was not found.","status":404,"detail":"Resource not found","instance":"/log-events/6cef99be-591e-42e4-b07c-34b45f9540df"}

断言脚本:

import groovy.json.JsonSlurper;
import java.util.regex.Pattern;

def failureMessage = "";
def jsonResponse = null;

JsonSlurper JSON = new JsonSlurper();

try {
  jsonResponse = JSON.parseText(prev.getResponseDataAsString());
  customerID = jsonResponse.customerId
} catch (Exception e) {
  def requestData = sampler.getUrl().toString();
  def pattern = Pattern.compile('^http?://localhost:8080/log/log-events/([^/]+)/.*$')
  def (_, refDATA) = (requestData =~ pattern)[0]
  failureMessage += refDATA
}
if (customerID.contains("Test") ||customerID.contains('KENDLE') || customerID.contains('CLINIRX') || customerID.contains('GSK')) {
  AssertionResult.setFailure(false);
} else {
        AssertionResult.setFailure(true);
        AssertionResult.setFailureMessage(failureMessage);

}

错误消息详细信息:

2021-08-13 10:40:21,803 ERROR o.a.j.a.JSR223Assertion: Problem in JSR223 script: ClientID_Validator
javax.script.ScriptException: java.lang.NullPointerException: Cannot invoke method contains() on null object
    at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:320) ~[groovy-jsr223-3.0.3.jar:3.0.3]
    at org.codehaus.groovy.jsr223.GroovyCompiledScript.eval(GroovyCompiledScript.java:71) ~[groovy-jsr223-3.0.3.jar:3.0.3]
    at javax.script.CompiledScript.eval(Unknown Source) ~[?:1.8.0_251]
    at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:222) ~[ApacheJMeter_core.jar:5.3]
    at org.apache.jmeter.assertions.JSR223Assertion.getResult(JSR223Assertion.java:50) [ApacheJMeter_components.jar:5.3]
    at org.apache.jmeter.threads.JMeterThread.processAssertion(JMeterThread.java:916) [ApacheJMeter_core.jar:5.3]
    at org.apache.jmeter.threads.JMeterThread.checkAssertions(JMeterThread.java:885) [ApacheJMeter_core.jar:5.3]
    at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:573) [ApacheJMeter_core.jar:5.3]
    at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:489) [ApacheJMeter_core.jar:5.3]
    at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:256) [ApacheJMeter_core.jar:5.3]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_251]

只想打印 UUID: 6cef99be-591e-42e4-b07c-34b45f9540df ,以防万一失败

我在这里错过了什么?

2个回答

我在您的 JSON 响应中没有看到任何 customerId 对象,因此您的行 customerID = jsonResponse.customerId 返回 null 并且当您稍后尝试调用 customerID.contains() 函数时,它会失败,因为 customerID 为 null。

如果没有看到成功的响应并且不知道您在成功的情况下想要实现什么,很难提出任何有意义的建议,作为一种解决方法,您可以为 customerID 添加额外的检查,例如:

import groovy.json.JsonSlurper

import java.util.regex.Pattern

def failureMessage = "";
def jsonResponse = null;

JsonSlurper JSON = new JsonSlurper();

try {
    jsonResponse = JSON.parseText(prev.getResponseDataAsString());
    customerID = jsonResponse.customerId
} catch (Exception e) {
    def requestData = sampler.getUrl().toString();
    def pattern = Pattern.compile('^http?://localhost:8080/log/log-events/([^/]+)/.*$')
    def (_, refDATA) = (requestData =~ pattern)[0]
    failureMessage += refDATA
}
if (customerID != null) {
    if (customerID.contains("Test") || customerID.contains('KENDLE') || customerID.contains('CLINIRX') || customerID.contains('GSK')) {
        AssertionResult.setFailure(false);
    } else {
        AssertionResult.setFailure(true);
        AssertionResult.setFailureMessage(failureMessage);

    }
}

有关使用 JSR223 断言的更多信息: 使用 Groovy 编写 JMeter 断言脚本 - 教程

Dmitri T
2021-08-13

您可以拆分 URL 并获取列表中的最后一项。 以下是从 URL 中提取 UUID 的示例代码

def requestData ="http://localhost:8080/log/log-events/6cef99be-591e-42e4-b07c-34b45f9540df"

def uuid = requestData.split("/").last()

println "uuid = $uuid"

Janesh Kodikara
2021-08-15