Reading values from propety files in RestAssured test



Hi, all welcome to another of my lockdown blog series posts.  In this blog post, I will discuss how we can overcome the use of hardcoded values by the use of properties files. In a previous blog, I discussed how we can make use of RequestSpecification to manage code that multiple tests may refer to. However, we can improve this further by moving the baseURL to a property file so that any change has to be done only in one location.

@BeforeTest
    public RequestSpecification requestSpec() throws java.io.IOException {

        GlobalVariables globalVariables=new GlobalVariables();

        log=new PrintStream(new FileOutputStream("new-log3.txt"),true);
        log.append("*********************TestResults**********************\r");

        RequestSpecification requestSpecification =new RequestSpecBuilder()
                .setBaseUri("=http://api.zippopotam.us")
                .setContentType(ContentType.JSON)
                .addFilter(new RequestLoggingFilter(log))
                .addFilter(new ResponseLoggingFilter(log))
                .build();
        return requestSpecification;
    }
 
To handle this situation, we need to follow several steps.
  1. Create a properties file that has the baseURL.
  2. Create a util class that reads the properties file.
  3. Pass the util class as a parameter to the Request Specification.
In your project move to the scr/test/resources folder and create a file with .properties
extension. Inside the file add the base URL as below

baseURL=http://api.zippopotam.us
 
Next, create a class and create an object of java.util.properties and java.io. FileInputStream and specify the path to the properties file within a static method as shown below. Next, call the load method of the java.util.properties and add the fileInputStream object into that as shown below.
 
package SpecBuilder;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Properties;

public class GlobalVariables {

    public static String getGlobalVariables(String key) throws java.io.IOException {
        Properties properties =new Properties();
        FileInputStream fileInputStream=new FileInputStream("/training/training/src/test/resources/resource.properties");
        properties.load(fileInputStream);
        return properties.getProperty(key);

    }
} 

As shown above, our util class is now ready and all we have to do is to call the getGlobalVariables method within the RequestSpecification. Since the method is declared as static we can directly call as shown in the below code example. As shown below the setBaseUri method is passed with the static method getGlobalVariables and passes the baseURL is the key to extract the value from the properties file.
 
 
package SpecBuilder;

import io.restassured.builder.ResponseSpecBuilder;
import io.restassured.filter.log.LogDetail;
import io.restassured.filter.log.RequestLoggingFilter;
import io.restassured.filter.log.ResponseLoggingFilter;
import io.restassured.http.ContentType;
import io.restassured.specification.RequestSpecification;
import io.restassured.builder.RequestSpecBuilder;
import io.restassured.specification.ResponseSpecification;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import java.io.FileOutputStream;
import java.io.PrintStream;
import static io.restassured.RestAssured.given;

public class SpecBuilderTest extends GlobalVariables{

    public static  PrintStream log;

    @BeforeTest
    public RequestSpecification requestSpec() throws java.io.IOException {

        log=new PrintStream(new FileOutputStream("new-log3.txt"),true);

        RequestSpecification requestSpecification =new RequestSpecBuilder()
                .setBaseUri(GlobalVariables.getGlobalVariables("baseURL"))
                .setContentType(ContentType.JSON)
                .addFilter(new RequestLoggingFilter(log))
                .addFilter(new ResponseLoggingFilter(log))
                .build();
        return requestSpecification;
    }

    public ResponseSpecification responseSpec()  {

        ResponseSpecification responseSpecification=new ResponseSpecBuilder()
                .expectStatusCode(200)
                .expectContentType(ContentType.JSON)
                .build();

        return  responseSpecification;
    }
    @Test
    public void testLKCode() throws java.io.IOException {

        given().
                spec(requestSpec()).
                when().
                get("/lk/10350").
                then()
                .spec(responseSpec());
    }

    @Test
     public void testUSCode() throws java.io.IOException {
        given().
                spec(requestSpec()).
                when().log().all().
                get("/us/90201").
                then()
                .spec(responseSpec());
    }

   
} 

Comments