REST Assured Request Parameters are a way of passing data for Objects that an API expects when it is called. Let’s look at an example API that expects Request Parameters and then write a REST Assured test for it.

Firstly, let’s take a look at how we would call that API using CURL:

curl http://localhost:8080/hello?name=qashahin

In the CURL above, we are hitting a local API endpoint called “hello” and then passing in the parameter “name” which has the value of “qashahin”. This then returns the following data:

{"name":"qashahin"}

Since the above data returns an array, let’s make the assumption that the endpoint above returned an object. In fact the API that returns the object above may look like this:

@RequestMapping("/hello")
public Person getPerson(@RequestParam("name") String name) {
    return new Person(name);
}

Notice that we are returning a “Person” as part of the API. The “Person” object would look like this:

public class Person {
    String name;

    Person(){};

    Person(String name) {
        this.name = name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return this.name;
    }
}

Now that we understand how to write call an endpoint and pass in Request Parameters, let’s take a look at how we may write a test.

@Test
public void shouldGetPersonName() {
    String name = "QAShahin";

    given()
        .param("name", name)
    .when()
        .get("/hello")
    .then()
        .body("name", is(name));
}

The test above should allow us to pass in a Request Parameter into an endpoint. Also, the “body” allows us to perform assertions on the results that are returned.

Please follow and like us:

Leave a Reply