Brief Idea about Functional Testing:-
Functional testing is basically the testing of component or application under test. In functional testing, we tend to cover all the actions and scenarios which user of the system can perform. In this approach, we focus mainly on two perspectives 1. Requirement based testing, 2.Business process-based testing. When it comes to the functional testing of API or web services then it becomes some different skill set and tools. For addressing this relatively new domain there are many tools and frameworks available in the market some of them are paid and some of them are open source and free to use.
What is REST Assured?
REST Assured is simple java library which is used to test REST services and create an automated script. It can be used for regression testing of REST services or one can use it for smoke testing, sanity testing depends on requirement. REST Assured is developed by JayWay company. This library can be used along with JUnit or TestNG testing framework to enhance test case maintainability and reporting.
Why REST Assured?
There are many reasons for choosing REST Assured for testing REST services. With all the below features one can handle automated REST API testing much easier.
- It provides simplicity like dynamic languages such as Ruby and Groovy.
- REST Assured working as the black box while testing one’s application.
- It supports all classic test runners such as JUnit or TestNG.
- We can write tests for the application written in any language such as Python,Ruby,Java,.Net ETC.
- Rest Assured is free to use and there is no explicit license required to use.
- It supports scala and groovy thus it can be used by various developer/QA who knows these various languages.
- Rest Assured can be used as Maven dependency.
- It supports POST, GET, PUT, DELETE, OPTIONS, and HEAD.
- It provides DSL-Like syntax.
- If someone needs to perform JSON Schema validation than it supports these and this is one of the cool features and provides basic validation.
- It provides Specification Reuse.
- Rest assured also provides easy file upload so one need not worry about this action and don’t need to write complex java code anymore.
Who should use REST Assured:-
REST Assured provides gherkin type syntax, individual(QA professional) will love if he is a BDD(Behaviour Driven Development) lover. As REST Assured is free to use and is based on Java, so it becomes the perfect candidate for used as REST API testing. As it is based on Java so one can get help from the huge Java community.
How to make POST Request with REST Assured:-
- Rest API URL:- URL of the Rest API Endpoint.
- API Body:- Request Body of the REST API Endpoint.
- Headers:- Pass the required Headers for requested Endpoint URL. Content-Type = application/json. Ex. setContentType() method.
- Authentication credentials;- Pass the username and password to the basic() method, leave basic() blank if there is no authentication. Ex. basic(“”,””)
Example:-
- Add the following dependencies in pom.xml:-
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<dependencies> | |
<dependency> | |
<groupId>com.google.code.gson</groupId> | |
<artifactId>gson</artifactId> | |
<version>2.6.2</version> | |
<scope>test</scope> | |
</dependency> | |
<dependency> | |
<groupId>io.rest-assured</groupId> | |
<artifactId>rest-assured</artifactId> | |
<version>3.0.7</version> | |
</dependency> | |
<dependency> | |
<groupId>io.rest-assured</groupId> | |
<artifactId>json-path</artifactId> | |
<version>3.0.7</version> | |
</dependency> | |
<dependency> | |
<groupId>io.rest-assured</groupId> | |
<artifactId>json-schema-validator</artifactId> | |
<version>3.0.7</version> | |
</dependency> | |
<dependency> | |
<groupId>io.rest-assured</groupId> | |
<artifactId>xml-path</artifactId> | |
<version>3.0.7</version> | |
</dependency> | |
<dependency> | |
<groupId>org.hamcrest</groupId> | |
<artifactId>hamcrest-all</artifactId> | |
<version>1.3</version> | |
<scope>test</scope> | |
</dependency> | |
<dependency> | |
<groupId>junit</groupId> | |
<artifactId>junit</artifactId> | |
<version>4.12</version> | |
</dependency> | |
</dependencies> |
2. Now create a java class and then annotate the test method with @Test annotation than use following basic syntax structure to make GET request:-
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FunctionalTest{ | |
@Test | |
public void setup(){ | |
baseURI = "<YOURENDPOINTURL>"; | |
String appkey = "<YOURAPPKEY>"; | |
given(). | |
header("AppKey", appkey). | |
when(). | |
get(baseURI). | |
then(). | |
statusCode(200) | |
.assertThat() | |
.body("payload.voyages.size", equalTo(45)) | |
.log().everything(); | |
} | |
} |
Conclusion:- As we know API/Web Services are gaining popularity in the current development scenario. There are many free and paid tools available for API testing but when it comes to the flexibility and rich support of the various readily available feature then RestAssured is surely a good choice. It has all the useful library and feature which enhances the API functional testing.
References:- http://rest-assured.io, https://stackoverflow.com,