REST Assured Tutorial 14 – BaseURI and BasePath – Introduction and Configuration

As a part of End to End REST Assured Tutorial , in this post We will learn about BaseURI and BasePath in Rest Assured.

We have already written CRUD operations in Rest Assured and you must have noticed I have used “baseUri()” method to set the API URI. You may have thought that API URI is Base URI which is not correct.

We all know about Google whose URL is ” https://www.google.com/“. Actually this is base URI of Google. Now append “/maps” in last of base url “https://www.google.com/” as “https://www.google.com/maps“. Google will launch Google Maps. Try same with “/news”. Google will launch Google News. Here “Maps” and “News” are actually resources which are called as base path. So to summarize, BaseURI is the address where different resources are located. A Base Path which is appended with BaseURI to locate a resource.

If we see RestFul Booker API doc, we can easily find BaseURI and BasePath:-

https://restful-booker.herokuapp.com/auth

https://restful-booker.herokuapp.com/booking

https://restful-booker.herokuapp.com/ping

Above, Underlined string is BaseURI and bold string is BasePath.

RestAssured provides multiple ways to setup BaseURI and BasePath. We can setup BaseURI and BasePath for individual request as well as for multiple requests at once.

Setting BaseURI and BasePath for individual request:-

To set for specific request, we can use methods provided by RequestSpecification interface. See an example below :-

RequestSpecification request = RestAssured.given();
request.baseUri("https://restful-booker.herokuapp.com");
request.basePath("/booking");

Setting BaseURI and BasePath at global level:-

If we have same BaseURI and BasePath for all or a group of requests , mentioning again and again for each request is not a good practice. Rest Assured provides global set up of BaseURI and BasePath so that it will be applicable for all required requests. We need to use RestAssured class for it. It provides static fields to set BaseUri and BasePath. See an example below:-

RestAssured.baseURI = "https://restful-booker.herokuapp.com";
RestAssured.basePath ="/booking";

We will see complete programs now.

Individual request level setup:-

package RestAssuredConcepts;

import org.testng.annotations.Test;

import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;

public class BaseURI_BasePath {

	/*
	 * We set same baseUri and basePath for both request.
	 */
	@Test
	public void Request1() {
		RequestSpecification request = RestAssured.given();
		// Setting Base URI
		request.baseUri("https://restful-booker.herokuapp.com");
		// Setting Base Path
		request.basePath("/booking");

		Response response = request.get();

		System.out.println(response.asString());
	}
	
	@Test
	public void Request2() {
		RequestSpecification request = RestAssured.given();
		// Setting Base URI
		request.baseUri("https://restful-booker.herokuapp.com");
		// Setting Base Path
		request.basePath("/booking");

		Response response = request.get();

		System.out.println(response.asString());
	}

	
}

Global setup :-

package RestAssuredConcepts;

import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;

public class BaseURI_BasePath_Global {

	
	
	@BeforeClass
	public void setup() {
		// Setting BaseURI once
		RestAssured.baseURI = "https://restful-booker.herokuapp.com";
		// Setting BasePath once
		RestAssured.basePath ="/booking";
	}
	
	@Test
	public void request1() {
		RequestSpecification request = RestAssured.given();

		Response response = request.get();

		System.out.println(response.asString());
	}
	
	@Test
	public void request2() {
		RequestSpecification request = RestAssured.given();

		Response response = request.get();

		System.out.println(response.asString());
	}

	
}

Output will be same for both programs:-

[RemoteTestNG] detected TestNG version 7.0.0
[{"bookingid":4},{"bookingid":5},{"bookingid":10},{"bookingid":3},{"bookingid":9},{"bookingid":8},{"bookingid":1},{"bookingid":2},{"bookingid":6},{"bookingid":7}]
[{"bookingid":4},{"bookingid":5},{"bookingid":10},{"bookingid":3},{"bookingid":9},{"bookingid":8},{"bookingid":1},{"bookingid":2},{"bookingid":6},{"bookingid":7}]
PASSED: request1
PASSED: request2

===============================================
    Default test
    Tests run: 2, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================


We can also store BaseUri and BasePath in Strings and use it wherever required. It will be helpful in managing and maintaining data.

package RestAssuredConcepts;

import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;

public class BaseURI_BasePath_Global2 {

	final String BASE_URI = "https://restful-booker.herokuapp.com";
	final String BASE_PATH = "/booking";
	
	
	@BeforeClass
	public void setup() {
		// Setting BaseURI once
		RestAssured.baseURI = BASE_URI;
		// Setting BasePath once
		RestAssured.basePath = BASE_PATH;
	}
	
	@Test
	public void request1() {
		RequestSpecification request = RestAssured.given();

		Response response = request.get();

		System.out.println(response.asString());
	}
	
	@Test
	public void request2() {
		RequestSpecification request = RestAssured.given();

		Response response = request.get();

		System.out.println(response.asString());
	}

	
}

You can clone/download example repo here.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappyLearning

You can find all Selenium related post here.
You can find all API manual and automation related posts here.
You can find frequently asked Java Programs here.

3 thoughts on “REST Assured Tutorial 14 – BaseURI and BasePath – Introduction and Configuration

Leave a Reply

Your email address will not be published. Required fields are marked *