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.

{
	"loginId": 1,
	"firstName": "TestFirstName",
	"lastName": "TestLastName",
	"email": "TestUser@simpletester.co.uk",
	"company": "simpletester.co.uk"
}

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.

public class Users {

    private Long loginId;
    private String firstName;
    private String lastName;
    private String email;
    private String company;

    public Long getLoginId() {
        return loginId;
    }

    public void setLoginId(Long loginId) {
        this.loginId = loginId;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
    }
}

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.

        
        <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.2</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils -->
        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.9.2</version>
        </dependency> 

Generating our Payload

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

import com.google.gson.Gson;
import org.apache.commons.beanutils.BeanUtils;
import org.junit.Test;

import java.lang.reflect.InvocationTargetException;

public class GeneratePayloadTests {

    @Test
    public void generateUserPayload() throws InvocationTargetException, IllegalAccessException {

        Users user = new Users();
        BeanUtils.setProperty(user, "loginId", "1");
        BeanUtils.setProperty(user, "firstName", "TestFirstName");
        BeanUtils.setProperty(user, "lastName", "TestLastName");
        BeanUtils.setProperty(user, "email", "TestUser@simpletester.co.uk");
        BeanUtils.setProperty(user, "company", "simpletester.co.uk");

        Gson gson = new Gson();
        String userPayload = gson.toJson(user);

        System.out.println(userPayload);
    }

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:

{
	"loginId": 1,
	"firstName": "TestFirstName",
	"lastName": "TestLastName",
	"email": "TestUser@simpletester.co.uk",
	"company": "simpletester.co.uk"
}
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 to Sana Cancel reply

Your email address will not be published.