Annotation Interface BeforeAll


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

In contrast to @BeforeEach methods, @BeforeAll methods are only executed once for a given test class.

Method Signatures

@BeforeAll methods must have a void return type, must not be private, and must be static by default. Consequently, @BeforeAll methods are not supported in @Nested test classes or as interface default methods unless the test class is annotated with @TestInstance(Lifecycle.PER_CLASS). @BeforeAll methods may optionally declare parameters to be resolved by ParameterResolvers.

Inheritance and Execution Order

@BeforeAll methods are inherited from superclasses as long as they are not hidden or overridden. Furthermore, @BeforeAll methods from superclasses will be executed before @BeforeAll methods in subclasses.

Similarly, @BeforeAll methods declared in an interface are inherited as long as they are not hidden or overridden, and @BeforeAll methods from an interface will be executed before @BeforeAll methods in the class that implements the interface.

JUnit Jupiter does not guarantee the execution order of multiple @BeforeAll methods that are declared within a single test class or test interface. While it may at times appear that these methods are invoked in alphabetical order, they are in fact sorted using an algorithm that is deterministic but intentionally non-obvious.

In addition, @BeforeAll methods are in no way linked to @AfterAll methods. Consequently, there are no guarantees with regard to their wrapping behavior. For example, given two @BeforeAll methods createA() and createB() as well as two @AfterAll methods destroyA() and destroyB(), the order in which the @BeforeAll methods are executed (e.g. createA() before createB()) does not imply any order for the seemingly corresponding @AfterAll methods. In other words, destroyA() might be called before or after destroyB(). The JUnit Team therefore recommends that developers declare at most one @BeforeAll method and at most one @AfterAll method per test class or test interface unless there are no dependencies between the @BeforeAll methods or between the @AfterAll methods.

Composition

@BeforeAll may be used as a meta-annotation in order to create a custom composed annotation that inherits the semantics of @BeforeAll.

Since:
5.0
See Also:
AfterAll, BeforeEach, AfterEach, Test, TestFactory, TestInstance