In this article written by Rupert Anderson, the author of SoapUI Cookbook, we will cover the following topics:
As you might already know, despite being called SoapUI, the product actually has an excellent RESTful web service mock and testing functionality. Also, SoapUI is very open and extensible with a great plugin framework. This makes it relatively easy to use and develop plugins to support APIs defined by other technologies, for example RAML (http://raml.org/) and Swagger (http://swagger.io/). If you haven’t seen it before, RESTful API Modeling Language or RAML is a modern way to describe RESTful web services that use YAML and JSON standards. As a brief demonstration, this article uses the excellent SoapUI RAML plugin to:
This article assumes that you have used SoapUI before, but not RAML or the RAML plugin. If you haven’t used SoapUI before, then you can still give it a shot, but it might help to first take a look at Chapters 3 and 4 of the SoapUI Cookbook or the Getting Started, REST, and REST Mocking sections at http://www.soapui.org/.
(For more resources related to this topic, see here.)
This recipe shows how to get the SoapUI RAML plugin installed and checked.
We’ll use SoapUI open source version 5.0.0 here. You can download it from http://www.soapui.org/ if you need it.
We’ll also need the RAML plugin. You can download the latest version (0.4) from http://sourceforge.net/projects/soapui-plugins/files/soapui-raml-plugin/.
First, we’ll download the plugin, install it, restart SoapUI, and check whether it’s available:
INFO:Adding [/Applications/SoapUI-5.0.0.app/Contents/Resources/app/bin/ext/raml-parser-0.9-SNAPSHOT.jar] to extensions classpath
INFO:Adding [/Applications/SoapUI-5.0.0.app/Contents/Resources/app/bin/ext/snakeyaml-1.13.jar] to extensions classpath
INFO:Adding plugin from [/Applications/SoapUI-5.0.0.app/Contents/java/app/bin/plugins/soapui-raml-plugin-0.4-plugin.jar]
In order to concentrate on using RAML with SoapUI, we won’t go into how the plugin actually works. In very simple terms, the SoapUI plugin framework uses the plugin jar file (soapui-raml-plugin-0.4-plugin.jar) to load the standard Java RAML Parser (raml-parser-0.9-SNAPSHOT.jar) and the Snake YAML Parser (snakeyaml-1.13.jar) onto SoapUI’s classpath and run them from a custom menu Action.
If you have Java skills and understand the basics of SoapUI extensions, then many plugins are quite straightforward to understand and develop. If you would like to understand how SoapUI plugins work and how to develop them, then please refer to Chapters 10 and 11 of the SoapUI Cookbook. You can also take a look at Ole Lensmar’s (plugin creator and co-founder of SoapUI) RAML Plugin blog and plugin source code from the following links.
If you read more on SoapUI plugins, one thing to be aware of is that open source plugins are now termed “old-style” plugins in the SoapUI online documentation. This is because the commercial SoapUI Pro and newer SoapUI NG versions of SoapUI feature an enhanced plugin framework with Plugin Manger to install plugins from a plugin repository (see http://www.soapui.org/extension-plugins/plugin-manager.html). The new Plugin Manager plugins are not compatible with open source SoapUI, and open source or “old-style” plugins will not load using the Plugin Manager.
In this section, we’ll use the SoapUI RAML plugin to set up a SoapUI REST service definition, mock service, and example response using an RAML definition file. This recipe assumes you’ve followed the previous one to install the RAML plugin and create the SoapUI Workspace and Project.
For this recipe, we’ll use the following simple invoice RAML definition:
#%RAML 0.8
title: Invoice API
baseUri: http://localhost:8080/{version}
version: v1.0
/invoice:
/{id}:
get:
description: Retrieves an invoice by id.
responses:
200:
body:
application/json:
example: |
{
"invoice": {
"id": "12345",
"companyName": "Test Company",
"amount": 100.0
}
}
This RAML definition describes the following RESTful service endpoint and resource:http://localhost:8080/v1.0/invoice/{id}.
The definition also provides example JSON invoice content (shown highlighted in the preceding code).
First, we’ll use the SoapUI RAML plugin to generate the REST service, resource, and mock for the Invoice project created in the previous recipe. Then, we’ll get the mock running and fire a test REST request to make sure it returns the example JSON invoice content provided by the preceding RAML definition:
Importing RAML from [file:/work/RAML/invoicev1.raml]
Assuming that you’re familiar with the basics of SoapUI projects, mocks, and test requests, the interesting part will probably be the RAML Plugin itself. To understand exactly what’s going on in the RAML plugin, we really need to take a look at the source code on GitHub. A very simplified explanation of the plugin functionality is:
Once the plugin has done its work, everything else is standard SoapUI functionality!
As you might have noticed under the REST service’s menu or in the plugin source code, there are two other RAML menu options or Actions:
To understand more about developing custom SoapUI Actions, Model items, and plugins, Chapters 10 and 11 of the SoapUI Cookbook should hopefully help, as also the SoapUI online docs:
Now that we have a JSON invoice response, let’s look at some options of testing it:
In this recipe, we’ll use option 3 and create a Script Assertion using JsonSlurper (http://groovy.codehaus.org/gapi/groovy/json/JsonSlurper.html) to parse and assert the invoice values that we expect to see in the response.
First, we’ll need to add a SoapUI TestSuite, TestCase, and REST Test Request TestStep to our invoice project. Then, we’ll add a new Script Assertion to TestStep to parse and assert that the invoice response values are according to what we expect.
import groovy.json.JsonSlurper
def responseJson = messageExchange.response.contentAsString
def slurper = new JsonSlurper().parseText(responseJson)
assert slurper.invoice.id=='12345'
assert slurper.invoice.companyName=='Test Company'
assert slurper.invoice.amount==100.0
com.eviware.soapui.impl.wsdl.teststeps.assertions.basic.GroovyScriptAssertion@6fe0c5f3
assert slurper.invoice.id==’12345′
| | | |
| | | false
| | 54321
| [amount:100.0, id:54321, companyName:Test Company]
[invoice:[amount:100.0, id:54321, companyName:Test Company]]
JsonSlurper is packaged as a part of the standard groovy-all-x.x.x,jar that SoapUI uses, so there is no need to add any additional libraries to use it in Groovy scripts. However, we do need an import statement for the JsonSlurper class to use it in our Script Assertion, as it is not part of the Groovy language itself.
Similar to Groovy TestSteps, Script Assertions have access to special variables that SoapUI populates and passes in when Assertion is executed. In this case, we use the messageExchange variable to obtain the response content as a JSON String. Then, JsonSlurper is used to parse the JSON String into a convenient object model for us to query and use in standard Groovy assert statements.
Another very similar option would have been to create a Script Assertion to use JsonPath (https://code.google.com/p/json-path/). However, since JsonPath is not a standard Groovy library, we would have needed to add its JAR files to the <SoapUI home>/java/app/bin/ext folder, like the RAML Parser in the previous recipe.
In this article, you learned about installing the SoapUI RAML plugin, generating a SoapUI REST project and mock service using the RAML plugin, and testing response values using JSON Slurper.
Further resources on this subject:
I remember deciding to pursue my first IT certification, the CompTIA A+. I had signed…
Key takeaways The transformer architecture has proved to be revolutionary in outperforming the classical RNN…
Once we learn how to deploy an Ubuntu server, how to manage users, and how…
Key-takeaways: Clean code isn’t just a nice thing to have or a luxury in software projects; it's a necessity. If we…
While developing a web application, or setting dynamic pages and meta tags we need to deal with…
Software architecture is one of the most discussed topics in the software industry today, and…