Generating JSON Payloads for your Tests


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

  1. Payload may be required as part of the actual automated test
  2. Set up steps where one may need to prepare an environment

There are a number of ways i have done this over the years, from having a JSON payload stored within my project, and then doing a find and replace on values to using string concatenation (very ugly).


What do we actually want to achieve?

So let’s start by identifying what we want as well as what we hope to achieve by the end of this post.  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 very simple 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 Google dependency allows us to generate our payload.

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:


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.