top of page
Search

Assertions in SOAP UI

Writer: Coding CampCoding Camp

What are Assertions?

An assertion is a boolean expression at a specific point in a program which will be true unless there is a bug in the program. An assertion could simply be a comment used by the programmer to think about how the code works. Or an assertion could document a constraint on the system.


Types of Assertion in SOAP:

Below are the ones that are available in Open Source version of SoapUI:

  1. Property Content

  2. Compliance Status Standard

  3. Script

  4. SLA

  5. JMS

  6. Security

In this article, we mainly focus on commonly used assertions. Let's dive in.



To create assertions, click on the '+' symbol in the request.


or by clicking the same icon under "Assertions" tab.


And Assertion(0) indicates there are no assertions added to the request.


1. Contains Assertion:

This Assertion searches for the existence of the specified string in the response XML. It also supports regular expression.

We will continue with the same example from the previous tutorial.


Here, we have the response XML holding the value of "106".



Now let's create a contains assertion.


click on contains-> click ok -> enter the value need to be validated and click ok


Now we could see, the assertion is added and passed and turned to green.


Now let's change the value and check. For that we click on the passed assertion "contains-VALID", and update the value and click ok.



Now we could see, the assertion failed and turned red.


2. Not Contains Assertion:

Let's create a Not contains Assertion by clicking Add Assertion Symbol.


Verify the assertion by entering the same value as in response. i.e. 106


Now we see the test fails along with the assertion, as the response contains the value 106.


Let's update the value which is not there in the response,

And the Not contains Assertion is passed.


3. XPATH Assertion:


Let's create XPATH Assertion,


Before Adding XPath, we need to declare the NameSpace.

An XML namespace is a collection of names, identified by a Uniform Resource Identifier (URI) reference, which are used in XML documents as element and attribute names. The same is used in SOAP UI XPath Assertion.


For declaring XML Namespace, we just need to click on 'Declare' button which would do the job for us else we can also manually declare a namespace ourselves.

Upon clicking the 'Declare' button, two namespaces will pop up as we have two URI's. One of them is the schema URL and the other one corresponds to the actual web service URL. We need to use the actual namespace where the web service is located and NOT the schema namespace while referencing XPath.


After declaring the namespace we need to refer the XPath using the created name space.

By clicking Select from current, we can retrieve the value from response XML automatically.

And click ok, and we could see the Assertion passed as the Expected value matches at the exact xpath, as we mentioned in the assertion.

Let's try out a negative scenario, by changing the xpath in assertion to something which is not there in the response.

By clicking select from current, a pop up appears saying "No match in current response" as there is no such xpath exists in response XML.

by clicking OK, we could see XPath assertion failed for the path exception.

4. Script Assertion:

Scripting assertions are used for creating user defined assertions that are NOT predefined by SOAP UI.

Let's create script Assertion,


The script Assertion dialog appears with scripting area and output pane. We can write groovy script to check any kind of value we want.

Let us see a small example groovy script to execute script Assertion,


//Define Groovy Utils and holder for validating the XML reponse content
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
def holder = groovyUtils.getXmlHolder(messageExchange.responseContent)

//Define the NameSpace
holder.namespaces["ns1"] = "http://tempuri.org/"

//Get the Value of the Node 'AddResult' and assign to a variable
def addResult = holder.getNodeValue("//ns1:AddResult")

//print the value of the result in the Output panel
log.info "The result value for integers is " + addResult

//Comparing the value to print 'Pass' or 'Fail'
if(addResult=="106")
{ log.info "Pass" }
else
{ log.info "fail"}

Here the script gets the value of AddResult from response XML and compare the value with 106, and print whether the test is passed or failed.


Execute the script by clicking play button and see the results in groovy test log.


For SLA assertion, check SLA Assertion in Soap UI.

 
 
 

Recent Posts

See All

Comments


bottom of page