JUnit 5 Test lifecycle mainly contains 4 primary annotations:
Apart from above 4 each test method must be marked with any one of the below annotations
- @Test
- @RepeatedTest
- @ParameterizedTest
- @TestFactory
- @TestTemplate.
Before and After cycle annotations
When we write JUnit test cases than in JUnit test life cycle usually we primarily need to have some methods to set up and tear down the environment or test data on which the tests run.
When we run JUnit test cases than for each test – a new instance of a test is created.
When we looked into @BeforeAll and @AfterAll annotations. It is very clear by their name itself it should be called only once in the entire tests execution cycle so they must be declared static.
On the other hand, @BeforeEach and @AfterEach are invoked for each instance of the test so they need not be static.
NOTE: If you annotate multiple methods with the same annotation (e.g. two methods with @BeforeAll or AfterEach ) then their execution order is undetermined.
Let’s try to understand JUnit 5 Test Lifecycle annotations using an example project
Project Structure
Utility Class for that we have to write JUnit Test:
1 2 3 4 5 6 7 8 |
package kkjavatutorials.com; public class MyUtils { public int add (int n1,int n2) { return n1+n2; } } |
JUnit Test Class with Life Cycle
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 |
package kkjavatutorials.com; import static org.junit.jupiter.api.Assertions.assertEquals; 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.Test; class MyUtilsTest { private static MyUtils myUtils; @BeforeAll static void setUp(){ System.out.println("Test data set up.."); myUtils = new MyUtils(); } @BeforeEach void beforeEach(){ System.out.println("@BeforeEach executed"); } @Test void addPositiveNumbersTest() { System.out.println("addPositiveNumbersTest Method.."); assertEquals(10, myUtils.add(6, 4)); } @Test void addNegativeNumbersTest() { System.out.println("addNegativeNumbersTest Method.."); assertEquals(-10, myUtils.add(-6, -4)); } @AfterAll static void tearDown(){ System.out.println("Test data tearDown.."); myUtils = null; } @AfterEach void afterEach(){ System.out.println("@AfterEach executed"); } } |
pom.xml
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 |
<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>kkjavatutorials.com</groupId> <artifactId>JUnitLifeCycleExample</artifactId> <version>0.0.1-SNAPSHOT</version> <name>JUnitLifeCycleExample</name> <properties> <maven.compiler.target>8</maven.compiler.target> <maven.compiler.source>8</maven.compiler.source> <junit.jupiter.version>5.5.2</junit.jupiter.version> </properties> <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>${junit.jupiter.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>${junit.jupiter.version}</version> <scope>test</scope> </dependency> </dependencies> </project> |
The output of the above project
You May Also Like:
What is JUnit?
JUnit 5 Annotations
JUnit 5 Maven Dependency
JUnit 5 with Gradle Dependency
JUnit 5 @BeforeAll annotation example
Unit 5 @AfterAll annotation example
JUnit 5 @BeforeEach and @AfterEach annotation Example
JUnit 5 Display Names
Assertions in JUnit 5 Examples
Third-party Assertion Libraries support in JUnit 5
JUnit 5 Assumptions Examples
JUnit 5 @Disabled Test Example
Conditional Test Execution in JUnit
Test Execution Order in Junit 5
JUnit 5 Nested Tests Example
Dependency Injection and Testing in JUnit 5
Test Interfaces and Default Methods in JUnit 5
JUnit 5 @RepeatedTest Annotation example
Junit 5 Parameterized Tests with examples
Argument Conversion in Parameterized Tests in Junit 5
Argument Aggregation in Parameterized Tests in Junit 5
Customizing Display Names of Parameterized Tests in JUnit 5
Dynamic Tests example in Junit 5
JUnit 5 Test Suites Examples
JUnit 5 Temporary Directory Support
How to execute Junit 5 tests in Eclipse IDE
JUnit 5 Test Templates for Eclipse
JUnit 5 vs JUnit 4
That’s all about JUnit 5 Test Lifecycle
If you have any feedback or suggestion please feel free to drop in below comment box.