
Beginner’s Guide to JUnit 5

JUnit 5 is a software testing framework used by the developers to unit test their code. It is a Java based framework that allows writing and running tests on the Java platform. It also includes an integrated reporter that can display the test results.
JUnit can be used for automation testing for two main purposes:
- Test that the software is working as it is expected to work
- Find and report errors in the code and fix them as quickly as possible
In this blog, we will be focusing on the latest version of JUnit that is JUnit 5.
What is JUnit 5?
JUnit 5 is the latest version of the JUnit testing framework. It supports Java 8 and above newer versions and accommodates different testing styles and offers a modern foundation for the developer side testing on the JVM.
The main benefit in using JUnit is its modularity as it contains multiple modules that allows the developers to test different components of the software easily. It can also be used to test individual classes of the application instead of the entire application.
Features of JUnit 5
The following are some of the popular and important features of JUnit 5 that distinguishes it from other unit testing frameworks
JUnit 4 v/s JUnit 5
With the release of the latest version of JUnit, i.e. JUnit 5, there are multiple changes and new features added to the framework. The differences between the two versions are listed in the table below:
Getting Started with JUnit 5
In this section, we will learn how to write the JUnit test cases using IntelliJ IDE. As it already comes bundled with JUnit, we don’t have to additionally perform any installation for using it.
We need to create a new Maven project and add the JUnit dependencies and start right away writing the tests.
Adding the JUnit Dependencies in the Maven project
The following JUnit 5 dependency needs to be added in the pom.xml file:
- JUnit Jupiter Engine
- JUnit Jupiter API
- JUnit Platform Suite(Aggregator)
- JUnit Jupiter Params
org.junit.jupiter
junit-jupiter-engine
5.11.4
test
org.junit.jupiter
junit-jupiter-api
5.11.4
test
org.junit.platform
junit-platform-suite
1.11.4
test
org.junit.jupiter
junit-jupiter-params
5.11.4
test
Here is the full pom.xml file
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">
4.0.0
io.github.mfaisalkhatri
junit-basic
1.0-SNAPSHOT
jar
junit-basic
http://maven.apache.org
UTF-8
org.junit.jupiter
junit-jupiter-engine
5.11.4
test
org.junit.jupiter
junit-jupiter-api
5.11.4
test
org.junit.platform
junit-platform-suite
1.11.4
test
org.junit.jupiter
junit-jupiter-params
5.11.4
test
After adding the dependencies, we are all set to write the test. But before we move on to writing the tests using JUnit 5 lets first understand the basic annotations that could help us in efficiently setting up the test suite and running tests as desired.
Basic Annotations used in JUnit 5
The following are basic annotations that are most popularly used by developers while writing the tests using JUnit 5.
@BeforeAll annotation
The @BeforeAll annotation indicates that the annotated method should be executed before any @Test, @RepeatedTest, @ParameterizedTest, or @TestFactory methods in the current class. It must be a static method in the test class.
@BeforeAll serves as the replacement for the @BeforeClass annotation used in JUnit 4.x
Syntax
public class BasicTest {
@BeforeAll
public static void beforeAllTest() {
System.out.println("Before All method called!!");
}
//..
}
@BeforeEach annotation
The @BeforeEach annotation indicates that the annotated method should be executed before each invocation of @Test, @RepeatedTest, @ParameterizedTest, or @TestFactory methods in the current class.
It is part of the test lifecycle methods and replaces the @Before annotation used in JUnit 4.
Syntax
public class BasicTest {
@BeforeEach
public void beforeEachTest() {
System.out.println("Before Each method called!!");
}
//...
}
@AfterAll annotation
The @AfterAll annotation indicates that the annotated method should be called after all the tests in the current class have been executed. It must be a static method and is used as a tear down method in the test class.
@AfterAll annotation is the replacement of @AfterClass annotation in JUnit 4.
Syntax
public class BasicTest {
//...
@AfterAll
public static void afterAllMethod() {
System.out.println("After All method called!!");
}
}
@AfterEach annotation
The @AfterEach annotation indicates that the annotated method should be executed after each @Test, @RepeatedTest, @ParameterizedTest, or @TestFactory method in the current class.
It serves as the JUnit 5 replacement for the @After annotation used in JUnit 4.
Syntax
public class BasicTest {
//...
@AfterEach
public void afterEachMethod() {
System.out.println("After Each method called!!");
}
}
@Test annotation
The @Test annotation indicates that the annotated method is a test method. The test method must not be a private or static method and must not return a value.
Syntax
public class BasicTest {
//..
@Test
public void testMethod() {
System.out.println("Test Method Called!!");
}
}
@DisplayName annotation
The @DisplayName annotation is used to specify a custom name for the annotated test class or method. These custom names can include spaces, special characters and even emojis and are typically utilized in the test reports generated by IDEs and build tools.
Syntax
@DisplayName("Demo Test class")
public class BasicTest {
//...
@DisplayName("This is a demo test")
@Test
public void testMethod() {
System.out.println("Test Method Called!!");
}
}
@Disabled annotation
The @Disabled annotation in JUnit 5 is used for excluding the test method or test class from the test suite for execution.
When applied to a test class, all the methods inside the test class are automatically considered as disabled. This annotation has an optional parameter that allows to specify the reason for disabling the test.
Syntax
public class BasicTest {
@Disabled("Test method disabled")
public void testMethod() {
System.out.println("Test Method Called!!");
}
//...
}
How to write automated test cases using JUnit 5?
It’s now time to get into the code and get our hands dirty with JUnit 5. We will be creating a new java class BasicTest.
public class BasicTest {
//..
}
All the the basic annotations that we discussed will be covered as a part of in the demonstration.
Let’s add methods in the BasicTest class to demonstrate the usage of @BeforeAll, @BeforeEach, @AfterAll, @AfterEach, @Test, @DisplayName and @Disabled annotations available in JUnit 5.
@DisplayName("Demo Test class")
public class BasicTest {
@BeforeAll
public static void beforeAllTest() {
System.out.println("Before All method called!!");
}
@BeforeEach
public void beforeEachTest() {
System.out.println("Before Each method called!!");
}
@DisplayName("This is a demo test")
@Test
public void testMethodOne() {
System.out.println("Test Method One Called!!");
}
@Test
@Disabled("This test is disabled to demo disable annotation")
public void disabledTest() {
System.out.println("This is a disabled test!!");
}
@Test
public void testMethodTwo() {
System.out.println("Test Method Two Called!!");
}
@AfterEach
public void afterEachMethod() {
System.out.println("After Each method called!!");
}
@AfterAll
public static void afterAllMethod() {
System.out.println("After All method called!!");
}
}
Code Walkthrough
The @DisplayName annotation over the test class will display the testname as “Demo test class” in the results when the test is executed.
The beforeAllTest() method will get executed before any of the test runs and will print the text “Before All method Called!!”. Next, the beforeEachTest() method will get executed before every test and print the text “Before Each Method called!!”.
There are 3 test methods in this class, namely, testMethodOne(), testMethodTwo() and disabledTest(). All of the three test methods have the @Test annotation over it that indicates that these are test methods.
The disabledTest() method will not be executed as it has the @Disabled annotation placed over it.
After every test execution is complete, the afterEachTestMethod() will be executed as it has the @AfterEach annotation placed above it.
Finally, after all the tests are run, the afterAllMethod() will be executed as it has the @AfterAll annotation placed over it.
Test Execution
The tests can be executed by Right Clicking on the test class and select Run ‘’(“BasicTest” in our case).
Another ways is to Click on the Green colored Play icon beside to the test class declaration
The following screenshot from IntelliJ IDE shows that the tests were executed successfully.
It can be seen in the above screenshot that the display name of the test class has been printed, also the disabled test can be seen as well and the reason for the disabled test is printed in the console.
Rest all the test methods including the beforetest and aftertest ones were executed successfully and the respective results were printed on the console.
Summary
JUnit 5 is a powerful testing framework used by developers to perform Unit Testing of the code. It is also used by test automation engineers to write and run the test automation scripts. It has rich features that allows developers and testers to write the test scripts and perform unit testing of the code.
It also provides multiple methods to perform assertions and verification that comes handy while writing automated tests. Along with writing and running the test scripts, it also provides a test report that can be used to demo the test execution results to the stakeholders to showcase the test coverage.
Happy Testing!!