VOOZH about

URL: https://www.geeksforgeeks.org/advance-java/beforeall-and-afterall-in-non-static-methods/

⇱ @BeforeAll and @AfterAll in Non-Static Methods - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

@BeforeAll and @AfterAll in Non-Static Methods

Last Updated : 23 Jul, 2025

In JUnit 5, the @BeforeAll and @AfterAll annotations allow setup and operations to be performed once before and after all test methods are executed in a test class. This is especially useful when handling expensive resources or operations that need not be repeated for each test.

Traditionally, methods annotated with @BeforeAll and @AfterAll need to be static. JUnit 5 introduced a way to use non-static methods for these annotations by using @TestInstance(Lifecycle.PER_CLASS). In this article, we will discuss the @BeforeAll and @AfterAll annotations in Non-Static Methods.

@BeforeAll Annotation

The @BeforeAll annotation marks a method to be run before any of the test methods in the test class. This method is executed only once per test class and is typically used for initializing shared resources or performing expensive setup logic that would otherwise need to be repeated for each test.

Non-Static @BeforeAll Method Example

In JUnit 5, if you use @TestInstance(TestInstance.Lifecycle.PER_CLASS), you can have non-static @BeforeAll methods. Here's how this works:

Explanation:

  • @TestInstance(TestInstance.Lifecycle.PER_CLASS): This annotation tells JUnit to create only one instance of the test class, so all test methods can share the same instance and its state. This is crucial for using non-static @BeforeAll methods.
  • Non-static @BeforeAll method: The initAll method runs before any test and initializes a shared resource. Since the test class has a single instance, this value can be used and modified across tests.
  • Shared resource across tests: Each test method modifies the shared resource initialized in the @BeforeAll method.

@AfterAll Annotation

The @AfterAll annotation marks a method to be executed once after all test methods in the current test class have run. It is typically used for cleanup tasks, such as releasing resources that were initialized in @BeforeAll or resetting states.

Non-Static @AfterAll Method Example

Here is an example of using a non-static @AfterAll method:

Explanation:

  • Non-static @AfterAll method: The tearDown method is run once after all the test methods have executed. It performs cleanup by resetting the shared resource.
  • Shared resource cleanup: Since the resource is shared across tests, the @AfterAll method can reset or clean up the resource to maintain consistency.

Difference between @BeforeAll and @AfterAll

@BeforeAll

@AfterAll

@BeforeAll indicates that the annotated method should run before all tests in the current test class.

@AfterAll is the method to be invoked after the completion of all tests in the current test class.

It can handle instance variables too. Methods can not be called directly.

Accessing methods as well as instance variables is possible here.

An instance is created every time for a test method in JUnit

Generation of only one instance is possible for all test methods.

An exception is thrown in BeforeAll if the method is not static (without @TestInstance).

No exception is thrown in AfterAll with @TestInstance(Lifecycle.PER_CLASS).

Key Points:

  • Static Requirement: By default, @BeforeAll and @AfterAll must be static because JUnit creates a new instance of the test class for each test method. Using @TestInstance(Lifecycle.PER_CLASS) allows these methods to be non-static by ensuring that a single instance of the test class is used.
  • Efficiency: The @BeforeAll and @AfterAll annotations optimize test execution by reducing setup and teardown overhead. This is especially useful for resource-heavy operations that don't need to be repeated for each test.

Conclusion

In this article, we explored how to use @BeforeAll and @AfterAll in non-static methods in JUnit 5. Normally, these methods need to be static, but by using @TestInstance(Lifecycle.PER_CLASS), you can make them non-static and reuse a single instance of the test class for all test methods. This is beneficial when you need to set up and tear down resources shared across tests, improving both test performance and code maintainability.

Comment
Article Tags:
Article Tags:

Explore