1

My motive is to assert the response from a JDBC call with an XML document which I have stored in 'Properties' test step. I need to validate some values.

I am trying to use Script type assertions for the JDBC Test step in SoapUI 5.2.1. I have previously created script type assertions for 'SOAP Request' Test steps too and they work fine. In case of JDBC, when I use :

def testCase = messageExchange.modelItem.testCase;

I get the error

"Cannot get property 'modelItem' on null object".

The same thing was used on a script assertion at a SOAP Request and it worked fine. Somehow, the implicit object 'messageExchange' is not available only for a JDBC Test step and it throws an NPE. enter image description here

Rao
  • 20,781
  • 11
  • 57
  • 77
chepaiytrath
  • 678
  • 1
  • 9
  • 20
  • Try to see `log.info messageExchange.metaClass.methods`, may `modelItem` not available. – Rao Aug 23 '16 at 05:26
  • @Rao When I tried logging the same, I faced a null error again. But once I removed messageExchange and just logged "metaClass.methods", a list of Object methods and some methods of class Script were logged on the screen. So messageExchange is still null. – chepaiytrath Aug 23 '16 at 10:53
  • Is it the case that you running the script assertion without actually running the jdbc test step? Because, the screen shot does not show any xml in the response editor. – Rao Aug 23 '16 at 10:54

1 Answers1

3

It is not true that messageExchange object is not available. Because, if you see top right corner of Script Assertion editor, it is evident that messageExchange object is available(as shown below).

Script is invoked with log, context, and messageExchange variables

Is it the case that you running the script assertion without actually running the jdbc test step?

In the script assertion, the test case properties can be access using below groovy statement:

def propValue = context.testCase.getPropertyValue('PROPERTY_NAME')
log.info "Property value is : ${propValue}"

The same above statement should work both in Groovy Script test step and in Script Assertion as well.

In case if you need test case object, then

Change from:

def testCase = messageExchange.modelItem.testCase;

To:

def testCase = context.testCase

EDIT:

From the comments, author of the question, requested to get the JDBC response from the Script Assertion.

In spite of the variables messageExchange is available in Script Assertion, looks it is only applicable to Soap or Rest type steps.

However, user should still be able to access the response in the Script Assertion using below statement:

import groovy.xml.*    
log.info context.responseAsXml
def responseSlurper = new XmlSlurper().parseText(context.responseAsXml)
log.info responseSlurper.ResultSet.Row.size()

enter image description here

Rao
  • 20,781
  • 11
  • 57
  • 77
  • I acknowledge the fact that the screen shows that **messageExchange** is available. But when I try to use it, it is recognized as a **null** object. Fetching the values from the particular **'Property' test step** can surely be achieved by using **context** as you already explained. But in order to fetch the response of the current **JDBC test step** in my assertion I am using : **def responseSlurper = new XmlSlurper().parseText(messageExchange.responseContent);** Here again I am using messageExchange which again is **null** – chepaiytrath Aug 23 '16 at 10:39
  • I believe your question is already answered. Ok, that is different question. Any way, please use `def responseSlurper = new XmlSlurper().parseText(context.response)` to get the same. Are you sure, you got the response in first place? Did you try the log statement that was provided earlier? – Rao Aug 23 '16 at 10:42
  • To begin with, using context.response did not give any error. BUT the slurper I printed had taken as input the response XML of a previous SOAP request by some way, when it should have been the simple JDBC response XML. And YES the JDBC response is not null. – chepaiytrath Aug 23 '16 at 10:56
  • That is strange. Does it even show the right response on `log.info context.response`? – Rao Aug 23 '16 at 11:07
  • No, the correct response is not shown. Instead of JDBC slurped data, it showed all the unnecessary data from the previous XML. At first I was happy that it worked without any error. Now not so much. It is as if context is some global variable. If you could, please explain the usage of these implicit variables : context, messageExchange, testRunner, etc. I am new to SOAPUI. I still will refer the web, but a hands-on description will go a long way – chepaiytrath Aug 23 '16 at 12:06
  • It worked!! I guess messageExchange is only available for SOAP/REST test steps. For JDBC, I would have to use context.responseAsXML. Thanks for the help bro. – chepaiytrath Aug 24 '16 at 03:28
  • Glad to know. Appreciate if up vote and accept as answer if the answer is helpful. – Rao Aug 24 '16 at 03:33