Unit 5 @AfterAll annotation example

By | December 6, 2019

In this post, we will talk and learn about the JUnit 5 life cycle @AfterAll annotation.

In JUnit 5 @AfterAll annotation is used to signal that the annotated method should be executed after all tests in the current test class.

It is a replacement for @AfterClass annotation in JUnit 4.

@AfterAll  annotated method must be declared as a static method otherwise it will throw runtime error.

@AfterAll  Annotation Example

Let’s understand @AfterAll Annotation using an example. Here I have MyUtils.java class with the add method. I will write a Junit test for this add method and we will see how we can make use of @AfterAll Annotation in the same Unit class

Utility Class for that we have to write JUnit Test:

JUnit Test Class, method setUp() is annotated with  @BeforeAll and method tearDown() is annotated with @AfterAll Annotation.

NOTE: Both setUp() and tearDown() method must be declared as static else it will throw Runtime Exception.

The output of the above test class

You May Also Like:

What is JUnit?
JUnit 5 Annotations
JUnit 5 Maven Dependency
JUnit 5 with Gradle Dependency
JUnit 5 Test Lifecycle
JUnit 5 @BeforeAll 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 @AfterAll annotation example

If you have any feedback or suggestion please feel free to drop in below comment box.

Leave a Reply

Your email address will not be published.