In this article, we will walk through some JUnit 5 annotations that often be used when we write tests.
1. Preparation
This section introduces some preparations you may need to set up if you want to get started with JUnit 5.
- JUnit 5 requires Java 8.
- You can set up environment for JUnit 5 with IDE like Eclipse, IntelliJ or with build tools like Maven, Gradle by following this link: Introduction To JUnit 5 – The Next Generation of JUnit
2. JUnit 5 Annotations
Below are some basic JUnit 5 annotations that we often use when we write tests.
JUnit 5 Annotations | Descriptions |
@BeforeEach | The annotated method will be run before each test method(annotated with @Test) in the current class. |
@AfterEach | The annotated method will be run after each test method (annotated with @Test) in the current class. |
@BeforeAll | The annotated method will be run before all test methods in the current class. |
@AfterAll | The annotated method will be run after all test methods in the current class. |
@Test | Declares a test method |
@DisplayName | Define custom display name for a test class or test method |
@Disable | Is used to disable or ignore a test class or method. |
@Nested | Used to declare nested test classes |
@Tag | Declare tags for test discovering and filtering |
@TestFactory | Denotes a method is a test factory for dynamic tests in JUnit 5 |
Below is an example of test class written by using JUnit 5 annotations
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.platform.runner.JUnitPlatform; import org.junit.runner.RunWith; @RunWith(JUnitPlatform.class) public class JUnit5TestExample { @BeforeAll static void initAll() { System.out.println("@BeforeAll - Run before all methods once"); } @BeforeEach void init() { System.out.println(" @BeforeEach - Run before each test methods "); } @DisplayName("First test") @Test void testMethod1() { System.out.println(" Test method 1"); } @Test @Disabled void testMethod2() { System.out.println(" Test method 2"); } @Test void testMethod3() { System.out.println(" Test method 3"); } @AfterEach void tearDown() { System.out.println(" @AfterEach - Run after each test methods "); } @AfterAll static void tearDownAll() { System.out.println("@AfterAll - Run after all test methods once"); } } |
Let’s see the console output when we run the test.
1 2 3 4 5 6 7 8 |
@BeforeAll - Run before all methods once @BeforeEach - Run before each test methods Test method 1 @AfterEach - Run after each test methods @BeforeEach - Run before each test methods Test method 3 @AfterEach - Run after each test methods @AfterAll - Run after all test methods once |
Note that the testMethod2 was disabled. So, it was not run.
Here is the test result when I run on my IntelliJ
3. Summary
We have seen some JUnit 5 annotations which are very basic and almost standard test classes often have. Note that if you’re familiar with the JUnit 4, you can see that all those annotations are different with the ones of JUnit 4. If you want to get to know the differences between JUnit 5 and JUnit 4 and other features of JUnit 5 as well, you can refer to my recent posts:
Introduction To JUnit 5 – The Next Generation of JUnit
JUnit 5 and Spring Boot Example
JUnit 5 Disable or Ignore A Test
JUnit 5 Dynamic Tests – Generate Tests at Run-time
JUnit 5 Test Suite – Aggregating Tests In Suites
JUnit 5 Assumptions With Assume
JUnit 5 Parameter Resolution Example