How to Integrate Rest Assured Test Framework for an Existing Git Repo With a Basic Rest Assured Test.

If you are willing to integrate the rest assured test framework for a wso2 repository you can follow the below steps to achieve that.

Assume that the git repository name is wso2-feedback & /home/shanika/WSO2/feedbackrepo is your machines folder path where you will clone the wso2-feedback repo.

git clone https://github.com/wso2/wso2-feedback.git

  1. Go to the most outer pom.xml file located in your repository and add the below dependency under the dependencies tag as below.

eg :- /home/shanika/WSO2/feedbackrepo/wso2-feedback/pom.xml

<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>${rest.assured.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>

2. In the same most outer pom.xml file which was used in step1 add the value for ${rest.assured.version} as below under the properties section.

<properties>
<!--Rest API test -->
<rest.assured.version>4.1.2</rest.assured.version>
</properties>

3. Next open the inner pom.xml file which is related to your test module and add the below dependency.

eg:- /home/shanika/WSO2/feedbackrepo/wso2-feedback/components/com.wso2.carbon.identity.feedback.mgt/pom.xml

<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>

4. Then add the folder structure to add a java class.

/home/shanika/WSO2/feedbackrepo/wso2-feedback/components/com.wso2.carbon.identity.feedback.mgt/src/test/java/com/wso2/carbon/identity/feedback/mgt/rest/assured/

(when giving directory names using . (dot) it will break the path into sub directories)

eg:- directory name > rest.assured

path will be generated as

/rest/assured/

5. Add a new java class under the path mentioned in step 4 as ApiMethodsTests.java

6. Import the below in the java class

import io.restassured.RestAssured;
import org.testng.annotations.Test;
import static io.restassured.RestAssured.given;

Note:- If the imported ones are not taken and shown with red as errors click on the option import maven changes which will be shown at the bottom of the Intellij IDEA or press alt + enter to auto-detect the fixes.

7. Now add a simple method with @ Test tag as below

public class ApiMethodsTests {
@Test
public void makeSureThatGoogleIsUp() {
given().when().get("http://www.google.com").then().log()
.all().statusCode(200);
}

8. Final code output will look like below.

package com.wso2.carbon.identity.feedback.mgt.rest.assured;
//import io.restassured.RestAssured;
//import org.testng.Assert;
import org.testng.annotations.Test;
//import io.restassured.http.Method;
//import io.restassured.response.Response;
//import io.restassured.specification.RequestSpecification;
//import static com.jayway.restassured.RestAssured.given;
//import org.testng.annotations.Test;
import static io.restassured.RestAssured.given;
//import org.junit.Test;

public class ApiMethodsTests {
@Test
public void makeSureThatGoogleIsUp() {
given().when().get("http://www.google.com").then().log()
.all().statusCode(200);
}

}

If you want to log the response you can use the .log().all() as shown above

9. Now open the testng.xml file and add the newly added test class there.

<classes>
<class name="com.wso2.carbon.identity.feedback.mgt.rest.assured.ApiMethodsTests"/>
</classes>

10. To execute the test and check the status navigate to the below location and execute the command mvn clean install

/home/shanika/WSO2/feedbackrepo/wso2-feedback

Senior Software Engineer and Freelance Technical Writer. I write about any Computer Science related topic. https://www.linkedin.com/in/shanikawickramasinghe