As we discussed in the previous article "Understanding Assertions in SoapUI", SoapUI supports various kinds of assertions that can be logically categorized based on whether they are available for the specific type of web service or not. Following the same, SoapUI supports specific assertions which are available only for REST services and will be enabled only when you are testing a REST webservice. Subsequently, in this article, we will cover details of all those SoapUI REST Assertions in the following topics:

  • What are Test specific assertions for REST services in SoapUI?
    • What is JsonPath Count Assertion in SoapUI?
    • Similarly, what is JsonPath Existence Match Assertion in SoapUI?
    • What is JsonPath Match Assertion in SoapUI?
    • Similarly, what is JsonPath RegEx Match Assertion in SoapUI?

What are SoapUI REST Assertions?

There are specific assertions in SoapUI which are applicable only to the REST services. Additionally, the majority of these assertions lie under the bucket of "Property Content Assertions" and highlighted in the following figure:

REST Service specific assertions in SoapUI

All of these assertions are the validations specific to the response of the REST service, which will always be JSON. As is viable from the above screenshot, the highlighted assertions are all specific to JSON validations only.

  • For understanding the details of these assertions, we will use the REST service URI: "http://bookstore.toolsqa.com/BookStore/v1/Books".
  • In addition to that, we will add a new project for this REST service as per the steps mentioned in the article "SoapUI: Working with Projects".
  • The sample output of the service will look as below:
{"books": [
      {
      "isbn": "9781449325862",
      "title": "Git Pocket Guide",
      "subTitle": "A Working Introduction",
      "author": "Richard E. Silverman",
      "published": "2013-08-02T00:00:00",
      "publisher": "O'Reilly Media",
      "pages": 234,
      "description": "This pocket guide is the perfect on-the-job companion to Git, the distributed version control system. It provides a compact, readable introduction to Git for new users, as well as a reference to common commands and procedures for those of you with Git experience.",
      "website": "https://chimera.labs.oreilly.com/books/1230000000561/index.html"
   },
      {
      "isbn": "9781449331818",
      "title": "Learning JavaScript Design Patterns",
      "subTitle": "A JavaScript and jQuery Developer's Guide",
      "author": "Addy Osmani",
      "published": "2012-07-01T00:00:00",
      "publisher": "O'Reilly Media",
      "pages": 254,
      "description": "With Learning JavaScript Design Patterns, you'll learn how to write beautiful, structured, and maintainable JavaScript by applying classical and modern design patterns to the language. If you want to keep your code efficient, more manageable, and up-to-date with the latest best practices, this book is for you.",
      "website": "https://www.addyosmani.com/resources/essentialjsdesignpatterns/book/"
   },
      {
      "isbn": "9781449337711",
      "title": "Designing Evolvable Web APIs with ASP.NET",
      "subTitle": "Harnessing the Power of the Web",
      "author": "Glenn Block et al.",
      "published": "2014-04-07T00:00:00",
      "publisher": "O'Reilly Media",
      "pages": 538,
      "description": "Design and build Web APIs for a broad range of clients—including browsers and mobile devices—that can adapt to change over time. This practical, hands-on guide takes you through the theory and tools you need to build evolvable HTTP services with Microsoft’s ASP.NET Web API framework. In the process, you’ll learn how design and implement a real-world Web API.",
      "website": "https://chimera.labs.oreilly.com/books/1234000001708/index.html"
   },
      {
      "isbn": "9781449365035",
      "title": "Speaking JavaScript",
      "subTitle": "An In-Depth Guide for Programmers",
      "author": "Axel Rauschmayer",
      "published": "2014-02-01T00:00:00",
      "publisher": "O'Reilly Media",
      "pages": 460,
      "description": "Like it or not, JavaScript is everywhere these days-from browser to server to mobile-and now you, too, need to learn the language or dive deeper than you have. This concise book guides you into and through JavaScript, written by a veteran programmer who once found himself in the same position.",
      "website": "https://speakingjs.com/"
   },
      {
      "isbn": "9781491904244",
      "title": "You Don't Know JS",
      "subTitle": "ES6 & Beyond",
      "author": "Kyle Simpson",
      "published": "2015-12-27T00:00:00",
      "publisher": "O'Reilly Media",
      "pages": 278,
      "description": "No matter how much experience you have with JavaScript, odds are you don’t fully understand the language. As part of the \"You Don’t Know JS\" series, this compact guide focuses on new features available in ECMAScript 6 (ES6), the latest version of the standard upon which JavaScript is built.",
      "website": "https://github.com/getify/You-Dont-Know-JS/tree/master/es6%20&%20beyond"
   },
      {
      "isbn": "9781491950296",
      "title": "Programming JavaScript Applications",
      "subTitle": "Robust Web Architecture with Node, HTML5, and Modern JS Libraries",
      "author": "Eric Elliott",
      "published": "2014-07-01T00:00:00",
      "publisher": "O'Reilly Media",
      "pages": 254,
      "description": "Take advantage of JavaScript's power to build robust web-scale or enterprise applications that are easy to extend and maintain. By applying the design patterns outlined in this practical book, experienced JavaScript developers will learn how to write flexible and resilient code that's easier-yes, easier-to work with as your code base grows.",
      "website": "https://chimera.labs.oreilly.com/books/1234000000262/index.html"
   },
      {
      "isbn": "9781593275846",
      "title": "Eloquent JavaScript, Second Edition",
      "subTitle": "A Modern Introduction to Programming",
      "author": "Marijn Haverbeke",
      "published": "2014-12-14T00:00:00",
      "publisher": "No Starch Press",
      "pages": 472,
      "description": "JavaScript lies at the heart of almost every modern web application, from social apps to the newest browser-based games. Though simple for beginners to pick up and play with, JavaScript is a flexible, complex language that you can use to build full-scale applications.",
      "website": "https://eloquentjavascript.net/"
   },
      {
      "isbn": "9781593277574",
      "title": "Understanding ECMAScript 6",
      "subTitle": "The Definitive Guide for JavaScript Developers",
      "author": "Nicholas C. Zakas",
      "published": "2016-09-03T00:00:00",
      "publisher": "No Starch Press",
      "pages": 352,
      "description": "ECMAScript 6 represents the biggest update to the core of JavaScript in the history of the language. In Understanding ECMAScript 6, expert developer Nicholas C. Zakas provides a complete guide to the object types, syntax, and other exciting changes that ECMAScript 6 brings to JavaScript.",
      "website": "https://leanpub.com/understandinges6/read"
   }
]}

  • Add a new TestCase to the same using the steps mentioned in the article "Creating and Structuring TestCases in SoapUI".
  • Additionally, click on the (+) icon on the Assertions tab at the bottom of the REST service TestStep screen to add a new assertion as per the steps mentioned in the article "Understanding Assertions in SoapUI".

We will perform all our validations for the JSON based assertions on the response as discussed above of the REST service.

Subsequently, let's understand the details of all these assertion types in the following sections:

What is JsonPath Count Assertion in SoapUI?

The JsonPath Count assertion helps in counting the occurrence of a string in the response. It applies to all the responses which return a  JSON as output.

Consider a scenario where we want to validate the number of books in the response of bookstore service. It should be "8" every time, which we can ascertain from the response of the target service. Additionally, we can put the assertion on this count, and the assertion shouldn't fail if the count returned by the service is 8.

Let's follow the steps mentioned below to validate the same using "JsonPath Count" assertion:

  1. First, navigate to Add Assertion dialogue box and click on the "JsonPath Count" assertion as highlighted below: Adding JsonPath count assertion for Rest Services in SoapUI

  2. Secondly, select the "JsonPath Count" assertion under the "Property Content" and click on the "Add" button. Consequently, it will show a dialogue box, as shown below:

Specify the expression and validation for the JsonPath Count assertion

  1. Thirdly, in the "JSONPath Expression", specify the string "books"(as shown by marker 1) and in the "Expected Result" section specify "8" (as indicated by marker) 2). Click the "Save" button to save the assertion. Consequently, it will show the result of assertion as passed, as designated below:

Result View for Rest Service JsonPath Count view assertion in SoapUI

  1. After that, as the count of "books" is "8" in the service response, so the assertion passed successfully. Following the same steps, we can add validation for the count of any of the critical values in the response.

What is JsonPath Existence Match Assertion in SoapUI?

The JsonPathExistence Match assertion helps us to find the existence of the key in the Response JSON.

Consider a scenario where we want to validate the existence of the key "isbn" in the response of bookstore service. It should return "true" as the key exists in the response of the bookStore service mentioned above. Therefore, it can ascertain by using the JsonPath Existence Match assertion in SoapUI.

Let's follow the steps mentioned below to validate the same using "JsonPath Existence Match" assertion:

  1. Firstly, from the "Add Assertion" dialogue box, add the "JsonPath Existence Match" assertion, as highlighted below:

Add JsonPath Existence Match assertion for Rest Services in SoapUI

  1. Secondly, after clicking on the "Add" button, it will show the "JSONPath Existence Match Configuration" dialogue box, as shown below :

Configure JsonPathExistence Match assertion in SoapUI

  1. Thirdly, in the "JsonPath Expression" section, specify "books[1].isbn" this will check for the existence of the "isbn" key in the 2nd element of the array response. Additionally, the result which you are expecting should be in "boolean (True/False)"in the "Expected Result" section. You must be seeing two buttons in this section:
  • 1. Test button: This button helps you to check the expression which you wrote is passing or failing. Moreover, if the JSON path (books[1].isbn) is in the response, you will see the below dialogue box :

Success response for the JSONPath Existence Match assertion in SoapUI

    1. Select from Current: It helps you to set the current value from the  JSON response (True/False). Once you click on "Select from the current button", it will fetch the boolean value based on the existence of the key as per the current response .
  1. Finally, once you finish this, you click on the "Save" button to save the assertion. It should pass as per the current response of the BookStore API.

What is JsonPath Match Assertion in SoapUI?

The JSONPath Match assertion uses a JSONPath expression to check the presence of any node and compare it to the value you expect.

Consider a scenario that we want to validate the value of the "publisher" field in the response of BookStore API mentioned above. As we can see from the code snippet showing the response of the REST service, there exists a "publisher" node in every object of the books array.

Let's follow the steps mentioned below to validate the same using "JsonPath Match" assertion:

  1. After navigating to the "Add Assertion" dialogue box, click on the "JsonPath Match" assertion as highlighted below :

Add JsonPath Match Assertion for Rest Services in SoapUI

  1. Clicking on the "Add" button will display the "JSONPath Match Configuration" dialogue box as below:

JsonPath Match configurations in SoapUI

Where,

  1. You need to specify the "JsonPath Expression," in our case, we will check the publisher of the book, which appears on the third number in the Array so that we will write the Json path as "books[2].publisher".
  2. You can specify the "Expected Result" as "O'Reilly Media".
  3. Select from Current: Once you are sure that the JSONPath mentioned above is correct, you can select the current value from the response by clicking on the button labeled as "3"  in the screenshot. So, instead of specifying the value manually in Step 2, we can click on this button to auto-populate the expected value.
  4. Click on the "Test" button highlighted as "3", and you should see the below notification in case of correct JSONPath.

JsonPath Success

  1. Once you complete the configuration, click on the "Save" button to add the Assertion.

Assertion

In our case, the assertion will pass, as shown above, as the expected value matched the key value in the response.

What is JsonPath RegEx Match Assertion in SoapUI?

The JsonPath Regex Match assertion works similarly to the JSONPath Match assertions. It helps us to verify the existence of the value of the particular node by using a regular expression instead of a fixed value.

Consider a scenario that you want to verify whether the first node of the Books Store API contains isbn and that isbn contains numbers only from 0-9.

Let's follow the steps mentioned below to validate the same using "JsonPath RegEx Match" assertion:

  1. From the "Add Assertion" dialogue box, select the "JSONPath RegEx Match" expression as highlighted below :

Selecting JsonPath RegEx Assertion for Rest Services in SoapUI

  1. Click on the Add button. After that, you will see the dialogue box  showing "JSONPath RegEx Match Configuration" as displayed  below :

JSONPath RegEx Match Configuration in SoapUI

  1. Here we will verify whether the "books[0].isbn" node contains the value from (0-9) Numerals only. So will put RegEx as "^[0-9]$*". It verifies that the value of the node in the JSON response is between 0-9 numbers. The specified regex verifies the following values:
  • ^  Indicates the start of the expression.
  • [0-9] Indicates the range of integers.
  • Indicates the number of characters (n in this case).
  • $ Indicates the end of the expression.
  1. Again, you can choose the expected result, either using the  "Select from current" button or specifying the "true" value manually.

  2. Click on the "Test" button, and you will see that this assertion  passes and it gives a success message  as below :

Success message for JSONPath RegEx Match assertion in SoapUI

  1. Click on the "OK" button, and you will again land on the configuration dialogue box showing the success of the assertion validation.

  2. Click on the "Save" button to add the assertion to the test step.

So, this completes details of all the SoapUI assertions, which we can apply for the validation of the response of the REST Webservices only.

Key Takeaways

  • SoapUI provides categories of assertions which are applicable only for the REST services.
  • JsonPath Count Assertion validates the count of a node in the response of the service
  • JsonPath Existence Match Assertion validates whether a node exists in the response of service or not.
  • Additionally, JsonPath Match Assertion is used to validate the exact value of a node in the response of the service.
  • JsonPath RegEx Match Assertion is used to validate the value of a node in the response of the service based on a regular expression.

Let's move to the next article, where we will under the details of "SOAP services Assertions in SoapUI".

Assertions in SoapUI
Assertions in SoapUI
Previous Article
SOAP Services Assertions in SoapUI
SOAP Services Assertions in SoapUI
Next Article

Similar Articles

Feedback