JUnit 5 @AfterAll

JUnit 5 @AfterAll annotation is a replacement of @AfterClass annotation in JUnit 4. It is used as tear-down method for the test class.

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

Note that to execute a method after each test, we can use @AfterEach annotation.

1. @AfterAll Annotation

Annotate a method with @AfterAll annotation as in the given example:

@AfterAll
public static void cleanUp(){
	System.out.println("After All cleanUp() method called");
}

Please remember that:

  • @AfterAll methods must have a void return type and must not be private
  • @AfterAll methods may optionally declare parameters to be resolved by ParameterResolvers.
  • @AfterAll methods are inherited from superclasses as long as they are not hidden or overridden. Furthermore, @AfterAll methods from superclasses will be executed before @AfterAll methods in subclasses.
  • @AfterAll annotated method MUST be a static method otherwise it will throw runtime error.
org.junit.platform.commons.JUnitException: @AfterAll method 'public void com.howtodoinjava.junit5.examples.JUnit5AnnotationsExample.cleanUp()' must be static.
	at org.junit.jupiter.engine.descriptor.LifecycleMethodUtils.assertStatic(LifecycleMethodUtils.java:66)
	at org.junit.jupiter.engine.descriptor.LifecycleMethodUtils.lambda$findAfterAllMethods$1(LifecycleMethodUtils.java:48)
	at java.util.ArrayList.forEach(ArrayList.java:1249)
	at java.util.Collections$UnmodifiableCollection.forEach(Collections.java:1080)
	at org.junit.jupiter.engine.descriptor.LifecycleMethodUtils.findAfterAllMethods(LifecycleMethodUtils.java:48)

2. @AfterAll Annotation Example

Let’s take an example. I have used one Calculator class and added one add method.

I will test the add method 5 times using @RepeatedTest annotation. This annotation will cause the add test to run 5 times. But @AfterAll annotated method must be called only once.

package com.howtodoinjava.junit5.examples;
 
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.RepetitionInfo;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;
 
@RunWith(JUnitPlatform.class)
public class AfterAnnotationsTest {
 
    @DisplayName("Add operation test")
    @RepeatedTest(5)
    void addNumber(TestInfo testInfo, RepetitionInfo repetitionInfo) 
    {
        System.out.println("Running test -> " + repetitionInfo.getCurrentRepetition());
        Assertions.assertEquals(2, Calculator.add(1, 1), "1 + 1 should equal 2");
    }
     
    @AfterAll
    public static void cleanUp(){
        System.out.println("After All cleanUp() method called");
    }
     
    @AfterEach
    public void cleanUpEach(){
        System.out.println("After Each cleanUpEach() method called");
    }
}

Where Calculator class is:

package com.howtodoinjava.junit5.examples;
 
public class Calculator 
{
    public int add(int a, int b) {
        return a + b;
    }
}

Now execute the test and you will see below console output:

Running test -> 1
After Each cleanUpEach() method called
 
Running test -> 2
After Each cleanUpEach() method called
 
Running test -> 3
After Each cleanUpEach() method called
 
Running test -> 4
After Each cleanUpEach() method called
 
Running test -> 5
After Each cleanUpEach() method called
 
After All cleanUp() method called

Clearly, @AfterAll annotated cleanUp() method is called only once.

Happy Learning !!

Was this post helpful?

Join 7000+ Awesome Developers

Get the latest updates from industry, awesome resources, blog updates and much more.

* We do not spam !!

Leave a Comment

HowToDoInJava

A blog about Java and related technologies, the best practices, algorithms, and interview questions.