In this tutorial, you will learn about JUnit5 Lifecycle methods. A Lifecycle Method is any method that is directly annotated or meta-annotated with @BeforeAll, @AfterAll, @BeforeEach, or @AfterEach. Let’s have a look at these annotations one by one.
When a method is annotated with @BeforeAll it means that the method should be executed before all @Test, @RepeatedTest, @ParameterizedTest, and @TestFactory methods in the current class.
When a method is annotated with @BeforeEach it means that the method should be executed before each @Test, @RepeatedTest, @ParameterizedTest, or @TestFactory method in the current class.
When a method is annotated with @AfterEach it means that the method should be executed after each @Test, @RepeatedTest, @ParameterizedTest, or @TestFactory method in the current class.
When a method is annotated with @AfterAll it means that it should be executed after all @Test, @RepeatedTest, @ParameterizedTest, and @TestFactory methods in the current class.
The pom.xml File
Below are the necessary dependencies that you will need to add to your project.
JUnit Jupiter is the combination of the new programming model and extension model for writing tests and extensions in JUnit 5. We need to add at least a single TestEngine implementation to our project to get started with the JUnit Platform.
Let’s say if we want to write tests with Jupiter, we will add the test artifact junit-jupiter-engine to the dependencies in pom.xml. After that, we can create tests in our test source directory.
We use Surefire Plugin to execute the unit tests of an application during the test phase of the build lifecycle. It generates reports in two different file formats that include Plain text files and XML files.
JUnit5 Lifecycle Method Examples
Now let’s see the working of the annotations discussed above to learn more about JUnit5 Lifecycle Methods. Here we have used @BeforeEach, @AfterEach, @BeforeAll, @AfterAll along with @Test . We have included simple print statements in our demo class so that we can understand the flow of execution of lifecycle methods.
Running JUnit test
Now let’s run the class as JUnit Test. Here we can clearly see that the method with @BeforeAll ran first, then the method with @BeforeEach annotation was executed before each test. Similarly, the method with @AfterEach annotation ran after each test. In the end method with @AfterAll annotation was executed.
Now let’s take another example. Here we have a Calculator class with add, multiply, divide and subtract methods. Now we will be implementing a test class for our Calculator class.
Following is CalculatorTest class. Let’s see what’s happening inside our test class.
- There is a method with @BeforeAll annotation with the name setUp . In this method, we are instantiating an object of the Calculator class along with a print statement. This method will run only once before all the methods.
- Then we have a method with name beforeEach() with annotation @BeforeEach . In this method, we have a simple print statement. Currently, it is not doing anything significant. But if there is a requirement we can add code that we need to run before each test method.
- Then we have methods with @Test annotations named testAdd(), testSub(), testMultiply(), and testDivide() . All these methods have assertEquals statement and a print statement. In the first argument, we have the expected output, then we are calling add, subtract, multiply and divide methods of the Calculator class with the reference of the object we instantiated in the setUp method that will return the actual output. Note that we can also display an error message in case the test fails.
- Then we have a method with annotation @AfterEach. It has a simple print statement that contains a message that will print after running each test method.
- In the end, the method with annotation @AfterAll will run. In this method, we are setting the Calculator object to null .
- Note that here we have used @DisplayName annotation. We use @DisplayName to declare a custom display name for the test class or test method.
Running JUnit test
Now when we run our test class as JUnit test, we can see the following statements printed on the console.
Here we can see that all our test methods have successfully passed as the expected output is equal to the actual output. We can also see the custom display names of test methods that we have provided using
With this, we have come to the end of our tutorial. In this tutorial, we learned about JUnit5 Lifecycle Methods. We looked at the annotations that we later used for implementing lifecycle methods. In the end, we did a hands-on and used the annotations in our demo classes.