DEV Community

Cover image for JUnit 5 - Introduction
Chathu Rashmini
Chathu Rashmini

Posted on

JUnit 5 - Introduction

What is JUnit 🤔

"JUnit is a simple framework to write repeatable test."
-JUnit Website-

JUnit provides a simple API to test our Java applications. A class or method that we want to test is known as Subject Under Test. To write a test we should create a Java Test Class and use JUnit API to make assumptions on the behaviors of that subject under test. So what JUnit does is verifying whether these assumptions are correct or not.

Architecture of JUnit 5

Basically JUnit is a combination of different modules.

  1. JUnit Platform: provides an API to launch the test from IDEs, Build Tools, or Console.
  2. JUnit Jupiter: provides an API to write JUnit tests and extensions.
  3. JUnit Vintage: provides a test engine implementation which provides backward compatibility if a test is written with JUnit 3 or 4.
  4. Third party testing frameworks: build their own APIs to write their tests and reuse JUnit platform to launch these tests.

JUnit Architecture

Writing Tests 💻

JUnit tests are nothing but methods inside the test class. JUnit identifies a method as a test when you use the annotation @test on top of the method.
Before getting our hands dirty please head to this GitHub repository where I have added the code required for you to follow up this tutorial. The LearnJUnitStarter folder contains the file set you need to have to get start with this tutorial. Once you clone it to your machine we are good to go 🤩

Project Background 🧐

What I have provided you with is a simple project that contains information - first name, last name, and contact number - of employees. In the file Employee.java you can see all the attributes, getters, setters, and methods related to the Employee class. There we validate that first name, last name, and contact number to be not empty. Plus we throw errors when a contact number does not have 10 digits, or if it contains non-digits, or if it does not start with '0'.
Next we have ManageEmployees.java file. There we have a method to add an employee. But before adding we check whether the employee is already available in the list and also we validate the employee attributes. Now I guess you have an idea on the project that I have given you. 😎

Write a simple test 👩‍💻

Yes, you are going to write your first test method in JUnit 🤩 We write our tests inside src -> main -> test -> java folder. There create a java class named TestManageEmployees.java. Now we are going to write tests inside it.

public class TestManageEmployees {
}
Enter fullscreen mode Exit fullscreen mode

As I mentioned above, we have a method called addEmployee. Let's write a test to that method. The usual way of naming test methods is adding the word Test in-front of the method name that you are going to test. In this scenario the name of the test method is TestAddEmployee.

public class TestManageEmployees {
    public void TestAddEmployee() {
    }
}
Enter fullscreen mode Exit fullscreen mode

In JUnit we have annotations to use to say that this method is a test. The annotation we should use here is @Test.

import org.junit.jupiter.api.Test;
public class TestManageEmployees {
    @Test
    public void TestAddEmployee() {
    }
}
Enter fullscreen mode Exit fullscreen mode

Now we should create an object from ManageEmployees class and use that object to add an employee.

import org.junit.jupiter.api.Test;
public class TestManageEmployees {
    @Test
    public void TestAddEmployee() {
        ManageEmployees employees = new ManageEmployees();
        employee.addEmployee("Edward", "Cullen", "0123456789");
    }
}
Enter fullscreen mode Exit fullscreen mode

As you can see above, we are to add a new employee named "Edward Cullen". (Yes, I am a Twilight Saga fan 🧛‍♀️ ) Now we should check whether the employees list is empty or its size is one. Process of verifying our expected output with the test output is know as Assertion. For that JUnit has provided us an Assertions class.

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class TestManageEmployees {
    @Test
    public void TestAddEmployee() {
        ManageEmployees employees = new ManageEmployees();
        employees.addEmployee("Edward", "Cullen", "0123456789");
        Assertions.assertFalse( employees.getEmployees().isEmpty() );
        Assertions.assertEquals(1, employees.getEmployees().size());
    }
}
Enter fullscreen mode Exit fullscreen mode

The line
Assertions.assertFalse( employees.getEmployees().isEmpty() );
we have assertFalse() method which takes in a Boolean parameter. If the Boolean is true, the method throws an exception and fails the test. So here we check whether the list of employees is null. If we have added the employee accurately you know the list cannot be null. Then we check the size of the list. As we have added only one employee we checks whether its size is 1, using the line,
Assertions.assertEquals(1, employees.getEmployees().size());

Now you can run the test by right clicking on TestAddEmployee and selecting Run 'TestAddEmployee()'.
If you followed up all the steps accurately you should get the following output.
1_TestAddEmployee

Kudos! Now you know how to write a test in JUnit!!! 🥳 If you are eager to write more complex tests, check the next tutorial as well 😃

Discussion (0)