Install Rest Assured Framework
- Open Intellij
- Go to File > New > Project
- Select Gradle and click next
- Give project name as Rest-Assured-API-Automation-Demo
- Click on the Finish button
- Now go to build.gradle file and add following lines:
// https://mvnrepository.com/artifact/io.rest-assured/rest-assured
testImplementation group: 'io.rest-assured', name: 'rest-assured', version: '4.3.3'
// https://mvnrepository.com/artifact/commons-configuration/commons-configuration
implementation group: 'commons-configuration', name: 'commons-configuration', version: '1.10'
7. Then click on import icon to install Rest Assured dependencies
Config environment variable:
8. Now create a file named config.properties under
src > test > resources folder
9. Add this line:baseUrl=https://customer-test-api.herokuapp.com
10. Then create a java class named Customer under test > java folder
11. Then Write following code:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Properties;
public class Customer {
Properties prop=new Properties();
FileInputStream file;
{
try
{
file = new FileInputStream("./src/test/resources/config.properties");
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
Calling POST API
Now we will call a POST API through Rest Assured Framework
URL: https://customer-test-api.herokuapp.com/customer/api/v1/login
Body:
{
"username":"salman",
"password":"salman1234"
}
After calling this API, a token will be generated that will be used to our next API’s
So, we will store the token in our config.properties file so that we can reuse it later
Write configuration file
12. Create a class named Utils under test> java folder
13. Write following code:
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import java.io.IOException;
public class Utils {
public static String setEnvVariable(String variable) throws IOException, ConfigurationException {
PropertiesConfiguration config = new PropertiesConfiguration("./src/test/resources/config.properties");
config.setProperty("token", variable);
config.save();
return variable;
}
}
setEnvVariable method will be used to save the generated token in config.properties file
14. Now Write following code inside Customer class:
public String token;
public void callingLoginAPI() throws IOException, ConfigurationException {
prop.load(file);
RestAssured.baseURI = prop.getProperty("baseUrl");
Response res =
given()
.contentType("application/json")
.body(
"{\"username\":\"salman\",\n" +
" \"password\":\"salman1234\"}"
).
when()
.post("/customer/api/v1/login").
then()
.assertThat().statusCode( 200 ).extract().response();
JsonPath jsonpath = res.jsonPath();
token = jsonpath.get("token");
Utils.setEnvVariable(token);
}
So, the full code will be:
import io.restassured.RestAssured;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;
import org.apache.commons.configuration.ConfigurationException;
import org.junit.Assert;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import static io.restassured.RestAssured.given;
public class Customer {
Properties prop=new Properties();
FileInputStream file;
{
try
{
file = new FileInputStream("./src/test/resources/config.properties");
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public String token;
public void callingLoginAPI() throws IOException, ConfigurationException {
prop.load(file);
RestAssured.baseURI = prop.getProperty("baseUrl");
Response res =
given()
.contentType("application/json")
.body(
"{\"username\":\"salman\",\n" +
" \"password\":\"salman1234\"}"
).
when()
.post("/customer/api/v1/login").
then()
.assertThat().statusCode( 200 ).extract().response();
JsonPath jsonpath = res.jsonPath();
token = jsonpath.get("token");
Utils.setEnvVariable(token);
}
}
15. Now create another java class named RunTest under test > java folder
16. Write following code:
import org.apache.commons.configuration.ConfigurationException;
import org.junit.Test;
import java.io.IOException;
public class RunTest {
@Test()
public void login() throws IOException, ConfigurationException {
Customer customer=new Customer();
customer.callingLoginAPI();
}
}
// @Test is JUnit annotation to define test method
17. Now, let’s run the project
18. Click on Add Configuration button on the top right corner of IDE
19. Select JUnit by clicking on + button
20. Set configuration following this screenshot:
21. Click apply and ok button
22. Now click on Run button on the top right corner of IDE
23. You will see that token has been generated and stored in config.properties file
Calling GET API
24. Now go to Customer class
25. Write following code inside Customer class:
public void customerList() throws IOException {
prop.load(file);
RestAssured.baseURI = prop.getProperty("baseUrl");
Response res =
(Response) given()
.contentType("application/json").header("Authorization",prop.getProperty("token")).
when()
.get("/customer/api/v1/list").
then()
.assertThat().statusCode( 200 ).extract().response();
JsonPath response = res.jsonPath();
Assert.assertEquals(response.get("Customers[0].id").toString(),"101");
System.out.println(res.asString());
}
So, the full code is:
import io.restassured.RestAssured;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;
import org.apache.commons.configuration.ConfigurationException;
import org.junit.Assert;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import static io.restassured.RestAssured.given;
public class Customer {
Properties prop=new Properties();
FileInputStream file;
{
try
{
file = new FileInputStream("./src/test/resources/config.properties");
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public String token;
public void callingLoginAPI() throws IOException, ConfigurationException {
prop.load(file);
RestAssured.baseURI = prop.getProperty("baseUrl");
Response res =
given()
.contentType("application/json")
.body(
"{\"username\":\"salman\",\n" +
" \"password\":\"salman1234\"}"
).
when()
.post("/customer/api/v1/login").
then()
.assertThat().statusCode( 200 ).extract().response();
JsonPath jsonpath = res.jsonPath();
token = jsonpath.get("token");
Utils.setEnvVariable(token);
}
public void customerList() throws IOException {
prop.load(file);
RestAssured.baseURI = prop.getProperty("baseUrl");
Response res =
(Response) given()
.contentType("application/json").header("Authorization",prop.getProperty("token")).
when()
.get("/customer/api/v1/list").
then()
.assertThat().statusCode( 200 ).extract().response();
JsonPath response = res.jsonPath();
Assert.assertEquals(response.get("Customers[0].id").toString(),"101");
System.out.println(res.asString());
}
}
26. Now go to RunTest class
27. Add following code:
@Test()
public void customerList() throws IOException {
Customer customer=new Customer();
customer.customerList();
}
28. Now click on Run button
29. You will see that the both API’s has been called and you will see the response
Run Test through Gradle:
30. Give following command:
gradle clean test
You will see that build has been succeeded and generates report
View Report
30. Go to build>reports>tests > test folder
You will see the report
The full project is here:
https://github.com/salmansrabon/rest-assured-api-testing-demo
Leave a Reply