0
0
JunitHow-ToBeginner ยท 3 min read

How to Use @AfterAll in JUnit: Syntax and Examples

In JUnit, use the @AfterAll annotation on a static method to run code once after all test methods in the class finish. This is useful for cleanup tasks like closing resources. The method must be static unless you use the @TestInstance(TestInstance.Lifecycle.PER_CLASS) annotation.
๐Ÿ“

Syntax

The @AfterAll annotation marks a method to be run once after all tests in the test class have completed. The method should be static by default. If you want to use a non-static method, annotate the class with @TestInstance(TestInstance.Lifecycle.PER_CLASS).

  • @AfterAll: Marks the method to run after all tests.
  • static: Required for the method unless using @TestInstance(TestInstance.Lifecycle.PER_CLASS).
  • Method signature: Must be void and take no parameters.
java
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test;

public class ExampleTest {

    @AfterAll
    public static void cleanup() {
        System.out.println("Cleanup after all tests");
    }

    @Test
    void testOne() {
        System.out.println("Test one running");
    }

    @Test
    void testTwo() {
        System.out.println("Test two running");
    }
}
๐Ÿ’ป

Example

This example shows a test class with two test methods and an @AfterAll method that prints a message after all tests finish. It demonstrates how @AfterAll runs once at the end.

java
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test;

public class AfterAllExample {

    @AfterAll
    public static void tearDown() {
        System.out.println("All tests completed. Cleaning up...");
    }

    @Test
    void firstTest() {
        System.out.println("Running first test");
    }

    @Test
    void secondTest() {
        System.out.println("Running second test");
    }
}
Output
Running first test Running second test All tests completed. Cleaning up...
โš ๏ธ

Common Pitfalls

Common mistakes when using @AfterAll include:

  • Not making the method static without using @TestInstance(TestInstance.Lifecycle.PER_CLASS), causing runtime errors.
  • Trying to use parameters in the @AfterAll method, which is not allowed.
  • Expecting @AfterAll to run after each test instead of once after all tests.

Here is an example of a wrong and right usage:

java
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;

public class PitfallExample {

    // Wrong: non-static method without @TestInstance
    @AfterAll
    public void wrongCleanup() {
        System.out.println("This will cause an error");
    }
}

// Correct approach using @TestInstance
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class CorrectExample {

    @AfterAll
    public void correctCleanup() {
        System.out.println("Runs after all tests, non-static allowed");
    }

    @Test
    void sampleTest() {
        System.out.println("Sample test running");
    }
}
๐Ÿ“Š

Quick Reference

Summary tips for using @AfterAll:

  • Use @AfterAll for cleanup after all tests in a class.
  • Method must be static unless using @TestInstance(TestInstance.Lifecycle.PER_CLASS).
  • Method signature: void methodName(), no parameters.
  • Runs once after all tests, not after each test.
โœ…

Key Takeaways

Use @AfterAll on a static method to run code once after all tests in a class.
If you want a non-static @AfterAll method, annotate the class with @TestInstance(TestInstance.Lifecycle.PER_CLASS).
The @AfterAll method must have a void return type and no parameters.
@AfterAll runs only once after all tests, not after each test method.
Common errors include forgetting static or misusing method signature.