Table of contents
1.
Introduction
2.
REST Assured
3.
REST Assured– Interface in OOPS
3.1.
Abstraction
3.2.
Implementation of Interface in OOPS
4.
Frequently Asked Questions
4.1.
What is RequestSpecBuilder?
4.2.
What is REST API testing?
4.3.
Can I create an object of an abstract class?
4.4.
Do we need to buy the license for REST Assured?
4.5.
Why is REST API called REST?
5.
Conclusion 
Last Updated: Mar 27, 2024
Hard

REST Assured– Interface in OOPS – Implement As You Wish

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Hello Coders!! Have you ever tested APIs? If yes, you must be familiar with the API (Application Programming Interface) testing tools. Today we are going to discuss one such tool, which is REST Assured. Further, we will also explore the interface in OOPS in Java.

Rest Assured OOPS concept

 REST (Representational State Transfer) is an architectural paradigm for establishing web services that define requirements. REST API is a straightforward and flexible approach to accessing online services without going through any processing.

Let's now move towards knowing more about REST assured.

REST Assured

Rest assured is a Java library that integrates well with Maven and enables us to test RESTful APIs. It was developed and maintained by Johan Halaby. It allows us to write readable, maintainable, and robust tests in Java for RESTful APIs. It is generally famous for testing JSON and XML-based web applications

REST Assured

Nearly every section of the request and response data can be retrieved using Rest Assured's techniques. The test for REST Assured runs on existing unit testing frameworks like TestNG or JUnit. It extracts many boilerplate code and makes your tests powerful and easy to read and maintain. 

REST Assured supports all REST methods, such as GET, PUT, POST, PATCH, and DELETE.

Must Read, Four Pillars of OOPS  

REST Assured– Interface in OOPS

Before we move on to implementing the interface in OOPS using REST Assured, we first need to understand the meaning of the term Abstraction

Interface in OOPS

The interface, as well as the abstract class and methods, are the ways to achieve Abstraction (Hiding) in Java. To achieve 100% abstraction, you should go with interfaces; otherwise, partial Abstraction can be achieved using abstract classes.

Abstraction

Abstraction in Java is one of the essential concepts of object-oriented programming. Abstraction in Java deals with hiding complex and unnecessary details from a user.

Let us understand this with an example. You switch between different channels on television using your TV remote, but have you ever wondered what going on inside the television remote whenever you press a button on it!! That's where Abstraction plays a significant role. See all the complex details of the working remote that have been made hidden from the user. In reality, many things happen inside the television remote with the press of a button. Still, you can use the remote easily without worrying about complex things happening inside it due to the beauty of Abstraction.

Abstraction is mainly used when we are concerned with 'what an application does' instead of 'how this application does.'

Implementation of Interface in OOPS

Now let us see how we can implement interface in OOPs using REST Assured. 

Step 1:-

First step is to create a project. For that, click on this link and generate a project like this-

Creating a project

Step 2:-

  1. Now extract the project file and import it into eclipse. For that-

Go to import->Maven->Existing Maven Project.

importing Maven

Now, a window will pop up where you have to give the root directory and click on finish. Hurray! The project is created!

Step 3:-

To use REST assured in your project, you must include the dependencies listed below. Now go to the pom.xml file, edit as shown below, and save it.

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

 
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
           <dependency>
                 <groupId>io.rest-assured</groupId>
                 <artifactId>rest-assured</artifactId>
                 <version>4.1.2</version>
            </dependency>
    <dependency>
     <groupId>javax.xml.bind</groupId>
     <artifactId>jaxb-api</artifactId>
     <version>2.3.0</version>
  </dependency>
 </dependencies>

Alright! We are done with setting up the project.

Step 4:-

Create an Interface with two abstract methods: printFirstName and printLastName.

package com.codingNinjas.AnuradhaDixit;

public interface Anuradha {
	 	void printFirstName();
	 	void printLastName();
}

Step 5:-

Create two classes that can implement the methods of the above interface.

ImplementInterfaceAnuradha.java 

package com.codingNinjas.AnuradhaDixit;

public class ImplementInterfaceAnuradha implements Anuradha{

	@Override
	public void printFirstName() {
		// TODO Auto-generated method stub
		System.out.println("Anuradha");
	}

	@Override
	public void printLastName() {
		// TODO Auto-generated method stub
		System.out.println("Dixit");
	}

}

ImplementInterfaceAnuradha2.java

package com.codingNinjas.AnuradhaDixit;

public class ImplementInterfaceAnuradha2 implements Anuradha{

	@Override
	public void printFirstName() {
		// TODO Auto-generated method stub
		System.out.println("Sagar");
	}

	@Override
	public void printLastName() {
		// TODO Auto-generated method stub
		System.out.println("Singh");
	}

}

Step 6:-

We will create a usage class that can use the above-created implemented class. In the given code, the return type is Anuradha which will return an object of ImplementInterfaceAnuradha.

package com.codingNinjas.AnuradhaDixit;

public class AnuradhaUsage {
	public static Anuradha method1() {
		return new ImplementInterfaceAnuradha();
	}
	
	public static Anuradha method2() {
		return new ImplementInterfaceAnuradha2();
	}
}

Note that in the above code, the return type of both methods has the same interface, Anuradha, but the actual content is different for both.

The first method returns an object of ImplementInterfaceAnuradha.java, and the second is ImplementInterfaceAnuradha2.java.

Step 7:-

Now create a test class that will call these methods to get the desired output.

package com.codingNinjas.AnuradhaDixit;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;

class AnuradhaTest1 {

	@Test
	public void test() {
		Anuradha a1=AnuradhaUsage.method1();
		a1.printFirstName();
		a1.printLastName();
		
		Anuradha a2=AnuradhaUsage.method2();
		a2.printFirstName();
		a2.printLastName();
	}

}

Output:-

output Rest Assured

Frequently Asked Questions

What is RequestSpecBuilder?

RequestSpecBuilder is a class in Rest Assured. It contains methods that help in setting cookies, multipart details, header, body, authentication, query parameters, form parameters, path parameters, base URI, base path, proxy, etc.

What is REST API testing?

REST API Testing is an open-source technique for testing RESTful APIs in web applications. It is commonly used to test XML and JSON-based web applications. It works with all methods, including PUT, POST, PATCH, GET,  and DELETE. REST API is a Java library.

Can I create an object of an abstract class?

No, You can’t create an object of an abstract class because abstract classes can contain abstract methods also.

Do we need to buy the license for REST Assured?

REST Assured is an open-source API testing tool, so there is no need to worry for the license.

Why is REST API called REST?

A REST API, also referred to as a RESTful API, is a representational state transfer API that complies with the constraints of the REST architectural style and enables communication with RESTful web services.  

Conclusion 

In this article, we have extensively discussed what REST assured is and how with the help of abstraction class and methods as well as an interface in OOPS, we can implement the Abstraction using REST Assured. To learn more about REST API and REST Assured, check the link below:

 

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.

Keep Coding!

Live masterclass