I added a BeanShell Assertion to my JMeter testcase. I want to check a JSON document in JMeter from an API.
My script looks like this:
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."
If i'm trying to start my testcase, i got the following response in my View Results Tree:
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.
How can i fix this issue?
There are multiple problems with your script:
Reference code:
See Groovy is the New Black article for more information on using Groovy with JMeter