NashTech Insights

API Testing | Containerizing Rest Assured API Tests with Docker

Soniya Raichandani
Soniya Raichandani
Table of Contents

In this blog post, we will show you how to containerize Rest Assured API tests with Docker.

What is API?

API testing is a critical aspect of ensuring the reliability and functionality of your applications. Rest Assured, a popular Java library provides a powerful framework for API testing. In this blog post, we will explore how to containerize Rest Assured API tests using Docker. By containerizing your tests, you can streamline the testing process, enhance test portability, and ensure consistent execution across different environments.

What is Docker and why do we Containerize?

Docker is a platform design and an open- source that allows automating different things like deployment, scaling, and management of applications using containerization. It provides an additional layer of abstraction and isolation, enabling you to package an application along with its dependencies, libraries, and configuration files into a standardized unit called a container.

A container is an environment for your code. Basically, a container has no knowledge of your OS(operating system as well as your files) It runs on the environment provided by Docker and it is a lightweight and standalone executable package that contains everything needed to run an application, including the code, runtime, system tools, system libraries, and settings. It encapsulates the application and its dependencies, ensuring consistency and portability across different computing environments, such as development machines, testing environments, and production servers.

Prerequisites-

We need these things in our system-
Java
Text Editor
Docker

How to install Docker on Linux

sudo apt-get update
sudo apt-get install docker-ce or sudo apt install docker.io
sudo snap install docker
docker --version

Verify that the Docker Engine with the “hello-world” image.

sudo docker run hello-world
sudo docker images

Images have been successfully pulled onto the system which means it is successfully working.

Create a RestAssured Project and Containerizing with Docker

Before creating a maven project, we need to install prerequisites-
1. Rest Assured
2. Java 11
3. Maven
4. TestNG
5. GitLab Account

Step 1. Configure REST-Assured Dependencies

After that create a maven project and add dependencies in the pom.xml file.

Maven dependency for rest assured 
<dependency>
    <groupId>io.rest-assured</groupId>
    <artifactId>rest-assured</artifactId>
    <version>4.3.0</version>
    <scope>test</scope>
</dependency>
Maven dependency for testng
<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>7.1.0</version>
    <scope>test</scope>
</dependency>

Step 2. Create a Docker File

In the root directory of your REST API project, create a file called “Dockerfile”. The Dockerfile contains instructions for building a Docker image for your API.

# Use a base image with Node.js
FROM node:14-alpine

# Set the working directory
WORKDIR /app

# Copy package.json and package-lock.json 
COPY package*.json ./

# Install project dependencies
RUN npm install

# Copy the rest of the application code 
COPY . .

# Expose the API port
EXPOSE 3000

# Set the command to start the API
CMD [ "npm", "start" ]

In the above Dockerfile, we start with a base image that has Node.js and Alpine Linux. We set the working directory, copy the package.json and package-lock.json files to the container, install the project dependencies using npm, copy the rest of the application code, expose the API port (in this case, 3000), and define the command to start the API.

Step 3: Build the Docker Image

Open a terminal or command prompt, navigate to the project’s root directory (where the Dockerfile is located), and execute the following command:

docker build -t your-api-image .

This command builds a Docker image based on the instructions in the Dockerfile and tags it with the name “your-API-image”. The period (“.”) at the end specifies the build context.

Step 4: Run the Containerized API

Once the image is built, you can run your containerized API using the following command:

docker run -p 3000:3000 your-api-image

This command starts a container from the “your-API-image” image and maps port 3000 of the container to port 3000 of your local machine. If necessary, you can replace the port numbers with the appropriate ones for your API.

Your REST API is now running inside a Docker container. It provides isolation, portability, and scalability for your API, making it easier to deploy and manage in different environments.

Conclusion

By combining the robust capabilities of Rest Assured and the containerization prowess of Docker, we can enhance the efficiency and reliability of our API testing workflows.

References:

https://www.freecodecamp.org/news/end-to-end-api-testing-with-docker/

Soniya Raichandani

Soniya Raichandani

Hi there! My name is Soniya Raichandani and I'm a Software tester with over 4.5 years experience.I have worked with a variety of testing methodologies and tools, including manual and automated testing, regression testing, and performance testing.I created this blog to share my knowledge and experiences with the testing community. My goal is to help other testers improve their skills and stay up-to-date with the latest trends in software testing.

Leave a Comment

Your email address will not be published. Required fields are marked *

Suggested Article

%d bloggers like this: