DEV Community

Khairun Nahar Nowrin
Khairun Nahar Nowrin

Posted on • Updated on

API Automation using JAVA, Dockerize and Write data from google sheets using Java for Generate API Test-case automation report

Environment Setup for JAVA API Project

To install the Java Development Kit (JDK) on a Mac, you can follow these steps:

Image description

Alternatively, you can also install JDK using Homebrew package manager by running the command

 brew cask install java
Enter fullscreen mode Exit fullscreen mode

Once the installation is complete, you can verify that the JDK is installed by opening a terminal window and running the command

java --version 
Enter fullscreen mode Exit fullscreen mode

This should display the version of the JDK that is currently installed on your system.

java --version                                              ✔  
javac 17.0.5
Which java  
Enter fullscreen mode Exit fullscreen mode

Image description

After that set the Java path in .zshrc file, To follow these steps:

  • Open the .zshrc file by running the command This will open
nano ~/.zshrc 
Enter fullscreen mode Exit fullscreen mode

the nano text editor with your .zshrc file.

  • Add the following lines to the file, replacing the path with the path to your JDK installation. To find Java path run
Which java 
Enter fullscreen mode Exit fullscreen mode

export JAVA_HOME=path-to-jdk
export PATH=$JAVA_HOME/bin:$PATH

  • Save and close the .zshrc file by pressing "CTRL+X", then "Y", and then "Enter".

  • Reload the .zshrc file by running the command

source .zshrc
Enter fullscreen mode Exit fullscreen mode
  • Verify the changes by running the command
echo $JAVA_HOME
Enter fullscreen mode Exit fullscreen mode

and "echo $PATH" it should display the path you have set in the above step.

Image description

Create API Project

To create a new Gradle project in IntelliJ IDEA, you can follow these steps:

  • Open IntelliJ IDEA and navigate to the "Welcome to IntelliJ IDEA" window.

  • Click on the "Create New Project" button or choose "File" > "New" > "Project" from the main menu.

  • In the "New Project" window, select the "Gradle" option from the list of available project types.

  • Select the "Java" or "Kotlin" option as the project's SDK. I select Java.

  • In the "Project name" field, enter a name for your project.

  • In the "Project location" field, choose a location on your computer where you want to save your project.

  • Click on the "Next" button to proceed.

  • In the "Project Structure" window, you can configure the settings for your project such as the project SDK, language level, and project format.

  • Click on the "Finish" button to create your new Gradle project.

Image description

Image description

Image description

Setup Project

API automation using Java involves writing code to test the functionality and performance of Application Programming Interfaces (APIs). There are several libraries and frameworks available in Java for API automation, including:

Rest-Assured: A Java library specifically designed for testing RESTful web services. It provides a simple and clean syntax for sending HTTP requests and asserting the responses.

JUnit: A popular unit testing framework for Java, JUnit can be used to write automated tests for APIs.

TestNG: Another testing framework for Java, TestNG is similar to JUnit but provides additional features such as support for data-driven testing and parallel test execution.

Apache HttpClient: A library for sending and receiving HTTP requests and responses. It can be used to send requests to an API and assert the response.

WireMock: A library for creating HTTP-based API mocks. It can be used to simulate API responses for testing purposes.

JSON: The JSON object library, such as Gson, provides a way to work with JSON data in Java by providing a set of classes and methods for parsing and generating JSON. Using a JSON library allows you to easily convert between JSON data and Java objects, making it more convenient to work with JSON data in a Java application.

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'

    //RestAssured
    testImplementation 'io.rest-assured:rest-assured:4.3.1'

    //TestNG Library
    testImplementation 'org.testng:testng:7.3.0'

    //HttpClient
    implementation 'org.apache.httpcomponents:httpclient:4.5.13'

    //WireMock
    testImplementation 'com.github.tomakehurst:wiremock-jre8:2.27.2'
    testImplementation 'com.github.tomakehurst:wiremock-standalone:2.27.2'

    //JsonObject
    implementation 'com.google.code.gson:gson:2.8.6'
}
Enter fullscreen mode Exit fullscreen mode

Code-base
Repository link - https://github.com/DevSK1207/API_Automation_and_App_Integratio_Testing_Using_Java
Run Test-case
I'm facing this issue while im run test case from Intellij.

Image description
To resolve this issue -> Intellij > settings > Build, execution >
To run a JUnit test case in Java, you can use the following command:

./gradlew test --tests <TestClassName>
Enter fullscreen mode Exit fullscreen mode

Can also specify the package name and class name to run the test case

./gradlew test --tests packageName.TestClassName
Enter fullscreen mode Exit fullscreen mode

Can also run all the test cases for a project by using

./gradlew test
Enter fullscreen mode Exit fullscreen mode

Dockerize the Project

FROM gradle:5.0-jdk11-alpine AS build


# Set the working directory to the project root
WORKDIR /app

# Copy the gradle
COPY --chown=gradle:gradle . /home/gradle/src

#Run gradle
RUN gradle build --no-daemon


FROM openjdk:11-jre-slim

EXPOSE 8080

# Copy the rest of the project source code
COPY . /JavaRestAssure

# Set the default command to run when the container starts

CMD ["java", "-jar", "build/libs/JavaRestAssure.jar"]
Enter fullscreen mode Exit fullscreen mode

For build docker Image

docker build -t java-automation .
Enter fullscreen mode Exit fullscreen mode

Run Docker container,

docker run -it java-automation /bin/bash
Enter fullscreen mode Exit fullscreen mode

Google Spreadsheet Setup for g-Sheet
Read and Write g-Sheet function
Write data from google sheets
Generate API Test-case automation report

Top comments (0)