我在我的JMeter测试用例中添加了BeanShell Assertion。 我想从API检查JMeter中的JSON文档。
我的脚本如下所示:
import groovy.json.*
def jsonText = '''
{
"message": {
"header": {
"from": "mrhaki",
"to": ["Groovy Users", "Java Users"]
},
"body": "Check out Groovy's gr8 JSON support."
}
}
'''
def json = new JsonSlurper().parseText(jsonText)
def header = json.message.header
assert header.from == 'mrhaki'
assert header.to[0] == 'Groovy Users'
assert header.to[1] == 'Java Users'
assert json.message.body == "Check out Groovy's gr8 JSON support."
如果我正在尝试启动我的测试用例,我在查看结果树中得到以下响应:
Assertion error: true
Assertion failure: false
Assertion failure message: org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval In file: inline evaluation of: ``import groovy.json.* def jsonText = ''' { "message": { "header": { . . . '' Encountered "def" at line 3, column 1.
我该如何解决这个问题?
答案 0 :(得分:1)
您的脚本存在多个问题:
参考代码:
def jsonText = '{\n' +
' "message": {\n' +
' "header": {\n' +
' "from": "mrhaki",\n' +
' "to": ["Groovy Users", "Java Users"]\n' +
' },\n' +
' "body": "Check out Groovy\'s gr8 JSON support."\n' +
' }\n' +
'}'
def json = new groovy.json.JsonSlurper().parseText(jsonText)
def header = json.message.header
if (header.from != 'mrhaki' || header.to[0] != 'Groovy Users' || header.to[1] != 'Java Users' || json.message.body != "Check out Groovy's gr8 JSON support.") {
AssertionResult.setFailure(true)
AssertionResult.setFailureMessage('There was a problem with JSON')
}
有关将Groovy与JMeter一起使用的更多信息,请参阅Groovy is the New Black文章