Automation Testing

REST Testing with Java Part Two: Getting Started with Rest-Assured

By Test Guild
  • Share:
Join the Guild for FREE

In my last post, we went over getting started with rest-assured. Now that we have everything set up, I'd like us to take a look at a few more examples of using Rest-Assured to test JSON REST services.

Preferred rest-assured BDD like Syntax

First I got a comment on my previous post from Johan Haleby, the founder of Rest-Assured (ßhow cool is that?!), who told me that the example I featured in my first post was the older way of writing tests.

He reminded me that the preferred and current way is to use the newer BDD (Behavior Driven Development) like given-when-then syntax. This is actually better for the framework that I'm creating, since we're also using BDD with Thucydides. So using the newer syntax will actually make our API tests look similar to our UI tests.

The Given/When/Then structure

When you create BDD features, the majority of its scenarios will be made up of three main Gherkin keywords: Given, When and Then. Each one has a particular function:

[1][2]

  • Given = a context/Getting a system in a particular state
  • When = something happens/Poke it
  • Then =we expect some outcome/examine the new state

Let's take a look at the example from the last post and change it to use the newer syntax:

Old Example


New Given-When-Then Example


I think using the newer syntax is a big improvement on the readability of the test.

Verifying multiple then values

Besides being able to verify one key value pair in the JSON response, we can also verify multiple values. Say for example we wanted to verify that the status code we get back is 200, which would indicate that the request succeeded, the status shows publish, and the author name equals gebdd.

Using these criteria, our tests will now look like the following:


One important thing to notice is that in order to obtain the name value, we used the dot notation (author.name) to navigate to the nested value we wanted in the JSON response. So, let's say you wanted to obtain the value contained in the self key. In that case you would use: author.meta.links.self


(I just want to point out that in the above example, the equalTo and containsString are examples of hamcrest matchers.)

Rest-Assured supports other requests

Rest-Assured also supports the following requests:

  • POST
  • GET
  • PUT
  • DELETE
  • OPTIONS
  • PATCH
  • HEAD

Stay tuned for more REST-Assured posts as we work our way through some POST requests in the next article.

Rest-Assured References:

  1. The Cucumber Book: Behavior-Driven Development for Testers and Developers
  2. BDD in Action – Behavior-driven development for the whole software lifecycle

REST-ASSURED – HOW TO POST A JSON REQUEST

REST-assured How to Check Response Times

REST Testing with Java Part Two: Getting Started with Rest-Assured

A bearded man with blue glasses and a black-and-white jacket smiles at a microphone in a studio setting.

About Joe Colantonio

Joe Colantonio is the founder of TestGuild, an industry-leading platform for automation testing and software testing tools. With over 25 years of hands-on experience, he has worked with top enterprise companies, helped develop early test automation tools and frameworks, and runs the largest online automation testing conference, Automation Guild.

Joe is also the author of Automation Awesomeness: 260 Actionable Affirmations To Improve Your QA & Automation Testing Skills and the host of the TestGuild podcast, which he has released weekly since 2014, making it the longest-running podcast dedicated to automation testing. Over the years, he has interviewed top thought leaders in DevOps, AI-driven test automation, and software quality, shaping the conversation in the industry.

With a reach of over 400,000 across his YouTube channel, LinkedIn, email list, and other social channels, Joe’s insights impact thousands of testers and engineers worldwide.

He has worked with some of the top companies in software testing and automation, including Tricentis, Keysight, Applitools, and BrowserStack, as sponsors and partners, helping them connect with the right audience in the automation testing space.

Follow him on LinkedIn or check out more at TestGuild.com.

  1. Hi,

    How to loop the below response if I want to validate the first “paymentMethodCode”: “CARD” ?

    “payments”: [
    {
    “status”: null,
    “paymentMethodCode”: “CARD”,
    “creationDateTime”: “2001-12-17T09:30:47Z”,
    “confirmationNumber”: “A1-2”,
    “authorizationAmount”: 200,
    “allocationAmount”: 200,
    “requestAmount”: 0,
    “cardNumber”: “123412card4123”,
    “sortCode”: “010203”,
    “accountNumber”: “12345678”,
    “chequeNumber”: “2345677”,
    “failureReason”: “Authorization fails”
    },
    {
    “status”: null,
    “paymentMethodCode”: “BANKPAYMENT”,
    “creationDateTime”: “2001-12-17T09:30:47Z”,
    “confirmationNumber”: “A1-2”,
    “authorizationAmount”: 200,
    “allocationAmount”: 200,
    “requestAmount”: 0,
    “cardNumber”: “123412card4123”,
    “sortCode”: “010203”,
    “accountNumber”: “12345678”,
    “chequeNumber”: “2345677”,
    “failureReason”: null
    }
    ]

  2. How can i write the code for the below if it has got multiple attributes:

    Eg: I want to validate the first “paymentMethodCode”

    “payments”: [
    {
    “status”: null,
    “paymentMethodCode”: “CARD”,
    “creationDateTime”: “2001-12-17T09:30:47Z”,
    “confirmationNumber”: “A1-2”,
    “authorizationAmount”: 200,
    “allocationAmount”: 200,
    “requestAmount”: 0,
    “cardNumber”: “123412card4123”,
    “sortCode”: “010203”,
    “accountNumber”: “12345678”,
    “chequeNumber”: “2345677”,
    “failureReason”: “Authorization fails”
    },
    {
    “status”: null,
    “paymentMethodCode”: “BANKPAYMENT”,
    “creationDateTime”: “2001-12-17T09:30:47Z”,
    “confirmationNumber”: “A1-2”,
    “authorizationAmount”: 200,
    “allocationAmount”: 200,
    “requestAmount”: 0,
    “cardNumber”: “123412card4123”,
    “sortCode”: “010203”,
    “accountNumber”: “12345678”,
    “chequeNumber”: “2345677”,
    “failureReason”: null
    }
    ]

Comments are closed.

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Exploring the Value of AI in Test Case Creation (Pros and Cons)

Posted on 03/10/2025

As testers, we're used to balancing test coverage with tight project deadlines. Right? ...

Solving the Top 6 Mobile Automation Challenges Fast (DevAssure Guide)

Posted on 02/27/2025

Are you tired of struggling with the mobile test automation setups? Does managing ...

Top Software Testing Conferences (2025 Guide)

Posted on 01/19/2025

Why a list of top software testing conferences? Many testers ask me what ...

Free Training: Supercharge Data Quality: Automate ETL & BI Testing with Agentic AI - Learn More >>