Generating JSON Payloads for your Tests

When writing automated tests, there are a number of reasons you might want to generate a JSON payload, the most common reasons for me over the past few years have been:

  1. An API request requires payload as part of an automated test
  2. Set up steps where you might need to prepare an environment

There are a number of ways I have done this over the years, from having a JSON file stored within my project, and then doing a find and replace on values, to using string concatenation, this seemed to work fine when the payloads where fairly static but can get very unsightly and difficult to work with when working with a payload that is required to change depending on the test.

What do we actually want to achieve?

So let’s start by identifying what we want to achieve.  Let’s assume we have a post request endpoint that creates users. So in order to test this endpoint, we will need to provide a JSON payload using a specified format. In this post, we will not be going over how to make an actual post request, simply the way in which we can generate a workable payload.

The payload below is a straightforward example of a user payload, in later posts, we can look at creating payloads that have multiple nodes.

Setting up the groundwork

We will be writing our code in Java, but as always the principles should apply across most languages.

The first thing to do is create a user class that contains all of the values we require as part of the payload. It’s also a good idea at this point to generate your Getters and Setters for these values.

In order to generate our payload, we will be utilising 2 libraries, the Google gson library as well as the Apache commons Beanutils. The BeanUlils dependency allows us to pass string values as our keys which can be very helpful when writing keyword-driven tests, and the gson dependency allows us to generate our payloads.

Add the following to your Maven pom, the equivalent Gradle dependencies can be found on Maven central.

Generating our Payload

We will now be creating a test class and using the above User class and libraries to generate our payload.

On line 12 we create a new object of our User class. We then proceed to add our values using the apache beanUtils libraries. The benefits of using the beanUtils library should now be fairly clear. The key (first value) can be passed in as a string and it will find the equivalent value from the User class.

On line 19 we instantiate the gson library after which we call the ‘toJson’ method in order to generate our payload.

On line 22 we are simply printing the output

Running the test, we get the following output:

Please follow and like us:

One Reply to “Generating JSON Payloads for your Tests”

  1. As someone who has an interest but very little knowledge on coding, I am impressed with the simplicity and how informative this post is. The methological style of your writing really highlights the significant parts of pulling data through. I myself have been experimenting with Python and I can see the similarities between the commands between the two languages. Keep up the good work!

Leave a Reply

Your email address will not be published.