An introduction about JUnit

JUnit is a framework used to write Unit Tests for Java projects. It has 2 popular versions, JUnit 4 and JUnit 5 with JUnit 5 version for Java projects using Java 8 onwards. In this tutorial, I will introduce you to the basic points that you need to know about this JUnit framework to use it to write Unit Tests!

First, I will create a new Maven project as an example:

In JUnit 4 version, we only need to declare 1 JUnit dependency, for example:

but from JUnit 5 version, JUnit divided into 3 modules are:

  • JUnit platform
  • JUnit Jupiter
  • and JUnit Vintage

so to use the latest JUnit version, you need to declare the dependencies as follows:

The differences between these modules are:

  • The JUnit platform is the core library of JUnit, responsible for launching Unit Test cases on the JVM. It also supports TestEngine APIs that make it possible to develop other testing frameworks that run on the JUnit platform.
  • JUnit Jupiter provides APIs and annotations for us to write Unit Tests.
  • JUnit Vintage is used to support running Unit Tests written using old version 3 or 4 of JUnit.

As an example, I will add a new Calculation class with the sum() method to calculate the sum of 2 numbers, with the following content:

To write a Unit Test for the Calculation class, I will create a new class named CalculationTest with the same package as the one containing the Calculation class but in the src/test/java directory as follows:

To add a Unit Test case to test the sum() method of the Calculation class, we will add a new method and annotate this method with the @Test annotation as follows:

You can initialize the Calculation object and then call the sum() method to test the sum() method in this testSum() method as follows:

We will use the assert…() methods included in the Assertions class to assert the information we want. In the above example, I am using the assertEquals() method to assert the return result of that sum() method, guys!

The results when I run this test case are as follows:

@BeforeAll and @BeforeEach annotation

When running Unit Test, you will have the need to do something like initialize an object for class, connect to a database, prepare data, … before running test cases. To do this, you can write methods and annotate them with the @BeforeAll and @BeforeEach annotations.

@BeforeAll will run before all test cases and @BeforeEach will run before each test case.

Result:

@AfterAll and @AfterEach annotation

Similar to @BeforeAll and @BeforeEach, we also have additional methods to do something after running each test case and all test cases with @AfterAll and @AfterEach annotations. Eg:

Result:

@Disabled annotation

You can disable certain Unit Test cases if you want by using the @Disable annotation, for example:

Test case testSum() will be ignored when we run Unit Test:

@DisplayName annotation

To change the display name of a test case or of the Unit Test class, you can use the @DisplayName annotation, for example, as follows:

Result:

At this point, I have introduced to you the basic knowledge, helping you to write Unit Tests for Java projects using JUnit. There are many related issues, we will find out together in the next tutorials!

Add Comment