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 per execution of a given test class. If the test class is annotated with@ClassTemplate, the@BeforeAllmethods are executed once before the first invocation of the class template. If a@Nestedtest class is declared in a@ClassTemplate, its@BeforeAllmethods are called once per execution of the nested test class, namely, once per invocation of the outer class template.Method Signatures
@BeforeAllmethods must have avoidreturn type 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). However, beginning with Java 16@BeforeAllmethods may be declared asstaticin@Nestedtest classes, in which case theLifecycle.PER_CLASSrestriction no longer applies. In addition,@BeforeAllmethods may optionally declare parameters to be resolved byParameterResolvers.Using
privatevisibility for@BeforeAllmethods is strongly discouraged and will be disallowed in a future release.Inheritance and Execution Order
@BeforeAllmethods are inherited from superclasses as long as they are not overridden according to the visibility rules of the Java language. 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 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