My new website on Artificial Intelligence and Machine Learning www.aimlfront.com

Dynamic response in SoapUI mock service using groovy script

Genearlly if any service is not availble to run and get response, we can create mock service using SoapUI. If that is mock obviously will get only one kind of response always with different request. But using groovy script can make different responses based out of request parsing.

Below see how many ways I created response from mock service.

1. Selecting a response based on the request

This script is specified at the MockOperation level and uses the script code to extract the input values from the incoming request. Based on some validations it returns the name of the MockResponse to return to the client. The script is as follows:
Aim of Script: In request we have name property based name property we are returning different responses like R1, R2 and R3.

  • If name is "AA" then response R1
  • Else if name is "BB" then response R2
  • Else response is R3.
def temp="XX";
def groovyUtils=new com.eviware.soapui.support.GroovyUtils(context)
def xml=new XmlSlurper().parseText(mockRequest.requestContent)
xml.breadthFirst().each{
        def v=it.toString()
        log.info("==============="+it.name()+"==================="+it.text());
        if(it.name()=="name"){
                temp=it.text();
                log.info("===================matching tag=========================="+it.text());
        }
}
//log.info("*temp**"+temp);
if(temp=='AA'){
        log.info("if R1");
        mockOperation.setDefaultResponse("R1");
}else if(temp=='BB'){
        log.info("else if Response 1");
        mockOperation.setDefaultResponse("R2");
}else{
        log.info("else Response 1");
        mockOperation.setDefaultResponse("R3");
}

paste scipt in soapui script console


2. Transferring values from the request to the response

This is straightforward scripting done at the MockResponse level; the script uses properties of the mockRequest object to get hold of the incoming request, extracts the desired values via XPath and then writes the created result to a requestContext property.

def temp="JavaVillage";
def groovyUtils=new com.eviware.soapui.support.GroovyUtils(context)
def xml=new XmlSlurper().parseText(mockRequest.requestContent)
xml.breadthFirst().each{
        def v=it.toString()
        if(it.name()=="name"){
                temp=it.text();
                log.info("============================================="+it.text());
        }
}
context.session=temp+' Munna'
mockOperation.setDefaultResponse("R1");

mock result using session in soapui


3. Read the response from a database for Mock Service

This one requires a bit more work as we need to set up and close a database connection which we can use in our scripts. This is best done in the MockService start script as follows

	import groovy.sql.Sql

	//def mockService=mockRunner.mockService 

	def sql=Sql.newInstance("HostName","username","password","databaseDriver");
	context.dbConnection=sql
	def row=sql.firstRow("select * from table")

	log.info "=================="+row.get("column1");
	context .responseMessage = row.get("column2");
	mockOperation.setDefaultResponse("R1");
	if(context.dbConnection!=null){
			log.info "Closing the connection";
			context.dbConnection.close()
	}

To know more details: Click HERE