MoinMoin Logo
  • Comments
  • Immutable Page
  • Menu
    • Navigation
    • RecentChanges
    • FindPage
    • Local Site Map
    • Help
    • HelpContents
    • HelpOnMoinWikiSyntax
    • Display
    • Attachments
    • Info
    • Raw Text
    • Print View
    • Edit
    • Load
    • Save
  • Login

Navigation

  • Start
  • Sitemap
Revision 12 as of 2020-05-03 12:52:40
  • SoapUI

SoapUI

SoapUI is a free and open source cross-platform Functional Testing solution.

http://www.soapui.org/

Rest OnRequest sample Groovy

  • http://www.soapui.org/apidocs/com/eviware/soapui/impl/rest/mock/RestMockRequest.html

  • http://www.soapui.org/apidocs/com/eviware/soapui/impl/rest/mock/RestMockResult.html

  • https://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletResponse.html

  • https://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html

import com.eviware.soapui.impl.rest.mock.RestMockResult

RestMockResult res = new RestMockResult( mockRequest )
String val ="{test:1234}"
mockRequest.httpResponse.writer << val
mockRequest.httpResponse.status = 200 
mockRequest.httpResponse.header = "Content-type: application/json"
mockRequest 
return res
  • http://www.soapui.org/scripting---properties/the-soapui-object-model.html

WAR generation from SoapUI project

SoapUI is able to generate a WAR to create mock servers to run on Tomcat. The SoapUI project is stored inside the WAR in WEB-INF/soapui/soapuiprj.xml .

Sample onRequest script to call groovy scripts inside WEB-INF/soapui/. They can be changed on runtime after Tomcat has exploded the WAR file.

Inside the onRequestScript

import groovy.util.GroovyScriptEngine
import com.eviware.soapui.support.GroovyUtils
def gru = new GroovyUtils(context)
GroovyScriptEngine ge = new GroovyScriptEngine(gru.projectPath)
Binding b = new Binding()
b.setVariable("mockRequest", mockRequest);
ge.run( "scriptx.groovy" , b )

scriptx.groovy

import com.eviware.soapui.impl.rest.mock.RestMockResult

RestMockResult res = new RestMockResult( mockRequest )
println "Message in tomcat console"
def servletContext = mockRequest.httpRequest.getServletContext()
println servletContext.getServerInfo()
println servletContext.getServletContextName()
servletContext.log("servlet context log") //INFO: in file logs\localhost.XXXX.log in apache

String paramx = mockRequest.httpRequest.getParameter("paramx")
String rendered = String.format("<html><body><p>Page with %s</p></body></html>",paramx)
mockRequest.httpResponse.writer << rendered
mockRequest.httpResponse.setStatus(200)
mockRequest.httpResponse.setContentType("text/html")
mockRequest.httpResponse.setContentLength( rendered.length() )

Install SOAP UI

  • wget https://s3.amazonaws.com/downloads.eviware/soapuios/5.5.0/SoapUI-x64-5.5.0.sh

  • sh SoapUI-x64-5.5.0.sh

Mock REST service

  • File, Create empty project
  • New REST Mock service
  • right mouse button, Add new mock action
  • add new mock response tothe action
  • define response {"key":"hello world"}
  • define status 200 and media type application/json
  • show rest mock service, click play green triangle
  • access http://localhost:8080/action1

  • https://www.soapui.org/soap-mocking/creating-dynamic-mockservices/

Dispatch script for action that has a Default response

Syntax highlighting not supported for 'grrovy', see HelpOnParsers.
   1 import com.eviware.soapui.impl.rest.mock.RestMockResult
   2 
   3 def getNumberArgument(){
   4   def requestPath = mockRequest.getPath()
   5   def split = requestPath.tokenize('/')
   6   
   7   def index =0
   8   for(item in split){   
   9         if(item=="number"){
  10                 break
  11         }
  12         index++
  13   }
  14   def nr = split[index+1].toInteger()
  15   return nr
  16 }
  17 
  18 def buildResponse(nr){
  19   def val =""
  20 
  21   if(nr % 2 ==0){
  22     val = '{"number":"evenxy"}'
  23   }
  24   else{
  25     val = '{"number":"oddxy"}'
  26   }
  27   log.info("called build resp")
  28   return val
  29 }
  30 
  31 def createMockResponse(status,contentType,payload){
  32   RestMockResult res = new RestMockResult( mockRequest )
  33   // HttpServletResponse
  34   // ServletResponse
  35   response = mockRequest.httpResponse
  36   response.setStatus(status)
  37   response.setContentType(contentType)
  38   response.writer.print(payload)
  39   response.writer.flush()
  40   return res
  41 }
  42 
  43 def nr = getNumberArgument()
  44 def val = buildResponse(nr)
  45 def res = createMockResponse(200,"application/json",val)
  46 
  47 return res
  • MoinMoin Powered
  • Python Powered
  • GPL licensed
  • Valid HTML 4.01