DEV Community

Cover image for API Testing from scratch. Setup test automation flow
Roman Orlov
Roman Orlov

Posted on

API Testing from scratch. Setup test automation flow

Welcome! This is a series of stories about API testing. For your convenience, I have added all of them to the separate list. Here, we will be learning the following things:

  • API structure

  • Test design and approaches to testing the API layer of an application

  • How to write extensible and readable code

  • Libraries that help us create a Test Automation Framework for API testing

  • Other helpful libraries

  • Contract testing

My goal here is to provide as many examples as possible and keep the articles short. This way, it will be convenient to save the articles and return to the required one if needed.

Feel free to join my Telegram channel as well, there I publish more thoughts about software testing and development.

Part 5. Starting a test automation project with Java

To start the first project, you need to install the following programs on your system:

  • Java SDK (JDK). You can find the instructions here. JDK allows you to run any Java program on your computer. Even though you might think that you don’t have a program, the tests themselves are programs that execute and verify something. Without JDK, you won’t be able to start at all.

  • Maven. You can find the instructions here. Maven is a build tool that allows you to manage project dependencies and control package versions. It is needed because any Java project is like a “Lego” — you use specific packages to solve separate tasks. These packages provide you with the features you need, and you just need to write code to use those features. In this article, we’ll see an example of such a package — the JUnit library, which is responsible for organizing the test flow.

  • IDE of your choice. I’ll develop the code with VSCode because it’s free and extensible. It also supports a lot of plugins for different programming languages and frameworks in a flexible way. However, if you don’t like it, feel free to choose another IDE such as Intellij IDEA, NetBeans, or Eclipse.

Once you’re done with the preparation, let’s move on to the next section.

Starting a new project

The detailed instructions are available at https://code.visualstudio.com/docs/java/java-tutorial. Just follow it and create a first file (Hello.java). Then Ctrl+Shift+P Java: Create Java Project command. In the list select Maven option:

Create a Java project. Step 1

and No Archetype then:

Create a Java project. Step 2

Next, we need to specify the root folder name for our code. Typically, it’s the same as the application we’re going to test, but in reverse. For example, if the application under test is gorest.co.in, the root folder name would be in.co.gorest. Please enter this name in the textbox.

Next, you’ll be prompted to provide the artifactId. For now, we don’t need to worry about what it is, as it will be discussed in a separate article in the future. Just think of this parameter as a project name, and choose any value you prefer. I’ve used “apitests” as my value, but feel free to choose whatever you like.

In the final step, VSCode will ask you to choose the folder where you want to store the project files. Select the correct folder, and you’ll have the following project structure:

Project structure

Adding test runner

Okay, the hardest part is over. Now it’s time to add the dependencies to our project.

Open the pom.xml file to edit it. This file describes the specifics of our project, such as the language version we use and the libraries included. First, let’s find and change the Java version we are using (initially it should be 1.8):

<properties>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
</properties>
Enter fullscreen mode Exit fullscreen mode

Now, we want to add the JUnit library (finally!). This library allows us to run tests, so it is essential for any automation project. To add it, we simply need to include a dependency in our project. Each specific dependency should be stored in the dependencies node of the XML document.

<dependencies>
    <dependency>
    // Need to specify a dependency here
    </dependency>
</dependencies>
Enter fullscreen mode Exit fullscreen mode

Now, let’s learn how to find a JUnit library to include. To do this, open the Maven Central Repository, which is a website that contains information about public packages. In the search field, type “JUnit” and open the corresponding result.

Maven central JUnit search results

In the central tab, you will find multiple versions of the library. Select the latest version and copy the code snippet for adding the Maven dependency (located in the Maven tab). Paste this snippet into the dependencies block of your pom.xml file. Your dependencies XML node should now look like this:

<dependencies>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.10.0</version>
        <scope>test</scope>
    </dependency>
</dependencies>
Enter fullscreen mode Exit fullscreen mode

And the full pom.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="<http://maven.apache.org/POM/4.0.0>"
    xmlns:xsi="<http://www.w3.org/2001/XMLSchema-instance>"
    xsi:schemaLocation="<http://maven.apache.org/POM/4.0.0> <http://maven.apache.org/xsd/maven-4.0.0.xsd>">
    <modelVersion>4.0.0</modelVersion>

    <groupId>in.co.gorest</groupId>
    <artifactId>api-tests</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.10.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>
Enter fullscreen mode Exit fullscreen mode

Create a test class

The test class is a special Java class that contains test scripts and can be run by JUnit (which we have already added). Let’s create the first test class in our project. Navigate to the src/test/java folder and create a new file named FirstTest.java.

Create a new file

Adding test flow annotations

Now we’re ready to prepare methods to start testing. Let’s explore the available methods. Copy and paste the following code into the created file:

package apitests.src.test.java;


public class FirstTest {
    @BeforeAll
    public static void globalSetup() {
        System.out.println("This is before class method");
    }
    @AfterAll
    public static void globalTeardown() {
        System.out.println("This is after class method");
    }

    @BeforeEach
    public void setup() {
        System.out.println("This is before each method");
    }
    @AfterEach
    public void tearDown() {
        System.out.println("This is after each method");
    }
    @Test
    public void test1() {
        System.out.println("This is test #1");
    }
    @Test
    public void test2() {
        System.out.println("This is test #2");
    }
}
Enter fullscreen mode Exit fullscreen mode

Here we have 2 dummy tests and several methods that should be executed before or after all tests in this class, or before or after test methods in this class. However, we currently have several errors.

To resolve this, let’s click on the “Testing” tab of VSCode and enable our tests for the JUnit Jupiter engine.

Setup VSCode for using JUnit. Step 1

Setup VSCode for using JUnit. Step 2

After installing all the necessary components, navigate to the error lines (annotations). Use the Ctrl+. combination to access the context menu when the cursor is on the annotation.

Resolve annotaion

Try this trick for every annotation present in the file.

Once all errors are fixed, we are ready to run our tests. Expand all the tests in the test explorer and click on the Play button for the entire class.

Running tests

If you have done everything correctly, you will see the results of the execution in the Debug Console in VSCode.

This is before class method
This is before each method
This is test #1
This is after each method
This is before each method
This is test #2
This is after each method
This is after class method
Enter fullscreen mode Exit fullscreen mode




Order of execution

Read our logs carefully. There are several important points here to understand:

  • The method marked with the BeforeAll/AfterAll annotation is executed only once before/after all tests in the class.

Before/After class methods

  • Methods marked with the BeforeEach/AfterEach annotation are executed before/after each automated test that we have written:

Before/After test methods

  • The methods with the Test annotation are executed in between other methods in the class.

What is the benefit of such a structure? Setup and Teardown methods are usually used for test preparation: reading configuration files to determine the testing environment, creating required test data, and cleaning it up after the test is passed, and setting up utility objects like HTTP clients. The advantage is that we avoid duplicated code in test methods, so we do not need to modify all tests if there are changes in the preparation logic.

Next steps

If your project is working fine (like mine 🙂), then we are done here and ready to add more dependencies to start API test automation.

Here is a useful link that can help you use Java with the VSCode IDE: Java in Visual Studio Code

Good luck and keep automating!

Top comments (1)