Annotation Type BeforeAll
-
@Target({ANNOTATION_TYPE,METHOD}) @Retention(RUNTIME) @Documented @API(status=STABLE, since="5.0") public @interface BeforeAll@BeforeAllis used to signal that the annotated method should be executed before all tests in the current test class.In contrast to
@BeforeEachmethods,@BeforeAllmethods are only executed once for a given test class.Method Signatures
@BeforeAllmethods must have avoidreturn type, must not beprivate, and must bestaticby default. Consequently,@BeforeAllmethods are not supported in@Nestedtest classes or as interface default methods unless the test class is annotated with@TestInstance(Lifecycle.PER_CLASS).@BeforeAllmethods may optionally declare parameters to be resolved byParameterResolvers.Inheritance and Execution Order
@BeforeAllmethods are inherited from superclasses as long as they are not hidden or overridden. Furthermore,@BeforeAllmethods from superclasses will be executed before@BeforeAllmethods in subclasses.Similarly,
@BeforeAllmethods declared in an interface are inherited as long as they are not hidden or overridden, and@BeforeAllmethods from an interface will be executed before@BeforeAllmethods in the class that implements the interface.JUnit Jupiter does not guarantee the execution order of multiple
@BeforeAllmethods 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,
@BeforeAllmethods are in no way linked to@AfterAllmethods. Consequently, there are no guarantees with regard to their wrapping behavior. For example, given two@BeforeAllmethodscreateA()andcreateB()as well as two@AfterAllmethodsdestroyA()anddestroyB(), the order in which the@BeforeAllmethods are executed (e.g.createA()beforecreateB()) does not imply any order for the seemingly corresponding@AfterAllmethods. In other words,destroyA()might be called before or afterdestroyB(). The JUnit Team therefore recommends that developers declare at most one@BeforeAllmethod and at most one@AfterAllmethod per test class or test interface unless there are no dependencies between the@BeforeAllmethods or between the@AfterAllmethods.Composition
@BeforeAllmay 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