Priya Priya - 1 year ago 225
Groovy Question

Groovy script to validate ResponseData in JMeter

I have written this script to verify field types, but i'm not sure if it is being validated correctly. Also i want to verify all the expected fields are seen.
This is my BSF Assertion:

enter image description here

import groovy.json.*;

def response = prev.getResponseDataAsString();
def json = new JsonSlurper().parseText(response)

def eventName = json.event_name
(eventName.getClass() == String)

def eventDate = json.event_start
(eventDate.getClass() == Date)

def attendeeLimit = json.attendee_limit
(attendeeLimit.getClass() == Integer)

def orderCount = json.order_count
(orderCount.getClass() == Integer)

def attendanceLimit = json.attendance_limit_on
(attendanceLimit.getClass() == String)

Answer Source
  1. If you want to check JSON response data types change your lines like

    (eventName.getClass() == String)


    assert eventName instanceof String

    See Groovy Testing Guide for details

  2. I would suggest switching fro BSF Assertion to JSR223 Assertion as it is able to compile the script and cache hence your script will perform much better. See Beanshell vs JSR223 vs Java JMeter Scripting: The Performance-Off You've Been Waiting For! article for comprehensive explanation and scripting best practices.

There is also a JSON Path Assertion available via JMeter Plugins, this one is mainly used to check response content

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download