Path Parameters

In projects, there are cases where we might have defined a particular base URL. And when we try to access in a particular endpoint other than the base URL then we might need to append the base URL.

In such cases, we can use a method called pathParam() method from the rest assured

@Test
void test() {		
	RestAssured.given()
	.pathParam("tail", "sample/api/product/read")
	.when()
	.get("https://chercher.tech/{tail}")
	.prettyPrint();
}

path-param-rest-assured

Query Parameters in RestAssured

We know that get() method is used to fetch the details from the server it could be products detail is it could be students detail it could be any details.

In some cases rather than fetching all the details, we might need to fetch only a few details or singular details. In such cases we might need to use queries along with the get method;

We can use the query along with the API endpoint to form the complete endpoint are we can use queryParam() method present in rest assured to form the query parameters.

Let's see how to to use query parameters along with the get itself rather than using queryParam().

@Test
void test() {
	RestAssured.given()
	.when()
	.get("https://chercher.tech/sample/api/product/read?id=2028")
	.prettyPrint();
}

sinle-rest-assured-product

We have seen the query parameter is appended along with API endpoint.

queryParam:

When you have one or two query parameters then it is easy to append along with the API endpoint but if you have more than two parameters then it becomes difficult to handle. In such cases, we will be using a queryParam() method from the rest assured.
query-params-rest-assured

Single parameter :

It below example showcases only one parameter

@Test
void test() {
	RestAssured.given()
	.queryParam("id", "2028")
	.when()
	.get("https://chercher.tech/sample/api/product/read")
	.prettyPrint();
}

query-param-get-method-rest-assured

Multiple Parameter:

You can add multiple parameters by adding one more queryParam()method.

RestAssured.given()
		.queryParam("id", "2028")
		.queryParam("name", "abcd")
		.when()
		.get("https://chercher.tech/sample/api/product/read")
		.prettyPrint();

Multiple Parameter in Single line:

We can pass multiple parameters in same method; instead of using queryParam() we have to use queryParams() method.

RestAssured.given()
		.queryParams("id", "2028", "name", "abcd") // multiple params
		.when()
		.get("https://chercher.tech/sample/api/product/read")
		.prettyPrint();

Parameters as Map:

We can pass the parameters as a map.

@Test
void test() {
	HashMap<String, String> map = new HashMap<String, String>();
	map.put("id", "2028");
	map.put("name", "abcd");
	
	RestAssured.given()
	.queryParams(map)
	.when()
	.get("https://chercher.tech/sample/api/product/read")
	.prettyPrint();
}

Static Reference of RestAssured class

Most of the methods present in the rest assured class are static methods when we are using the static methods in Java we can reference to Static methods using the class.

At the same time if you are importing those static methods as static import then no need to use the reference of the class. We can directly use those static methods in our code

In the below example we are going to use static methods using class reference

import org.junit.jupiter.api.Test;
import io.restassured.RestAssured;
public class FirstRestAssuredTest {
	@Test
	void sampleTest() {
		RestAssured.given() // class reference
		.when()
		.get("https://chercher.tech/sample/api/product/read.php")
		.then()
		.statusCode(200);
	}
}

static-reference-rest-assured

So now let's re-write the differences by using the static import. You don't have to think the static import as a very big thing the only simple thing we are going to add is a Keyword called static in front of the package that we are going to import;

import static io.restassured.RestAssured.*; // static import
public class FirstTest {
		
	@Test
	void test() {
		given() // without class reference
		.queryParam("", "")
		.when()
		.get("https://chercher.tech/sample/api/product/read.php")
		.then()
		.statusCode(200);
	}
}

static-import-rest-assured

You might not find any difference between these two programs has the output as the same, the only difference is how we import those packages.

About Author :

I am Pavankumar, Having 8.5 years of experience currently working in Video/Live Analytics project.

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions