0
votes

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.

Screenshot

How can i fix this issue?

Edit: Screenshot JSR223 Assertion Screenshot2

1
Try to use JSR223 Assertion with the same code but choose groovy as language - ararar
If i'm trying the script in a JSR223 Assertion with language groovy, i got no feedback to the JSR223 Assertion in my Results tree. But the test have to be failed. - fcb1900
can you explain why it has to fail, like if you change (assert header.to[1] == 'Java Users') to (assert header.to[1] == 'Java Users123123') it will fail. - ararar
The test has to fail, because the structure of the JSON Document from the API is diffrent to my groovy test-script. But in JSR223 Assertion i got no response about the test result. No win or fail response. - fcb1900
I see, i think you should check this question, you can restructure your groovy code to validate the json. stackoverflow.com/questions/39280950/… - ararar

1 Answers

1
votes

There are multiple problems with your script:

  1. Your JSON is not a valid one, you need to escape quotes
  2. Groovy assert keyword won't cause assertion failure, it will only print exception into jmeter.log file, if you need to fail assertion itself you need to use AssertionResult shorthand instead

Reference code:

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')
}

See Groovy is the New Black article for more information on using Groovy with JMeter