REST Assured path parameters are a way of testing parameters in an API. In this blog post, we will look at how we can pass in parameters using a REST Assured test for an API that expects it.

Firstly, let’s take a look at how an API may look like when it expects a path parameter:

https://thetestroom.com/hi?name=qashahin

The URL above has a few parts to it. It firstly has the secure protocol of https followed by a domain name (i.e. thetestroom.com). After that, it has an endpoint called “hi” which then expects a parameter called “name”. In the name parameter, we have provided it with a value of “qashahin”.

Now let’s have a sneaky look at what the endpoint may look like in a Java API:

@RequestMapping("/hi/{name}")
public String sayHello(@PathVariable("name") String name) {
    return "Hi " + name;
}

The code above matches to the code of our “hi?name” endpoint above. If we wanted to write a REST Assured test for this, we may write our test like so:

@Test
public void shouldGetStringOfHi() {
    String name = "QAShahin";
    given()
        .pathParam("name", name)
    .when()
        .get("hi/{name}")
    .then()
        .body(is(String.format("Hi %s", name)));
}

The test above follows a Gerkin style of test providing a Given, When and Then approach. In the code above we firstly provide a path parameter in the “pathParam” method and then call it in the “get()”.

Finally, we perform assertions. You should now be able to write REST Assured tests by providing path parameters.

Please follow and like us:

Leave a Reply