Rest Assured with TestNG

Dilshani Subasinghe
2 min readMar 4, 2019

I was working with a test automation framework with cucumber, selenium and TestNG.

According to the requirement, had to have hybrid test architecture to cater to use cases. Hence decided to do REST API testing with Rest Assured.

Framework:

  • Maven based Java project
  • BDD method used via Cucumber
  • Selenium for automating UI use cases
  • TestNG used to run tests

Requirement:

  • Integrating REST API related automation tests

Solution:

  • Integrate Rest Assured Library and using it within the framework

Maven Dependency

<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>json-schema-validator</artifactId>
<version>3.3.0</version>
</dependency>

Remember to add <scope>test</scope> within above dependency, if you want to limit your scope.

Mock REST API

Test Class Implementation

I have used TestNG to run cucumber classes as follows:

@BeforeClass(alwaysRun = true)
public void setUpClass() throws Exception {
testNGCucumberRunner = new TestNGCucumberRunner(this.getClass());
}

@Test(groups = "cucumber", description = "Runs Cucumber Feature", dataProvider = "features")
public void feature(CucumberFeatureWrapper cucumberFeature) {
testNGCucumberRunner.runCucumber(cucumberFeature.getCucumberFeature());
}

@DataProvider
public Object[][] features() {
return testNGCucumberRunner.provideFeatures();
}

@AfterClass(alwaysRun = true)
public void tearDownClass() {
testNGCucumberRunner.finish();
Reporter.loadXMLConfig(new File(FileReaderManager.getInstance().getConfigReader().getReportConfigPath()));
}
}

According to my framework, I will be writing a test case inside of cucumber step definitions. If you need a reference for direct integration of Rest Assured with TestNG to follow these [2, 3].

Test Class With Rest Assured

//API URL
RestAssured.baseURI = "http://www.mocky.io/v2/5c78ed70300000a42d49b04d";

//HTTP request
RequestSpecification httpRequest = RestAssured.given();
//Getting response
Response response = httpRequest.request(Method.GET);
//Response response = httpRequest.request(Method.GET, "/city");

//Getting response body to verify/assert
String responseBody = response.getBody().asString();
System.out.println("Response Body is => " + responseBody);
System.out.println(response.getStatusCode());

For more functionalities in REST Assured and easy validations for JSON responses please refer this [1].

Enjoy resting more with REST Assured Testing ;)

References:

[1] https://github.com/rest-assured/rest-assured/wiki/Usage

[2] https://www.toolsqa.com/rest-assured/configure-eclipse-with-rest-assured/

[3] https://techbeacon.com/app-dev-testing/efficient-api-testing-how-get-started-rest-assured

--

--