Skip to content

Commit f510e82

Browse files
committed
Document execution order and wrapping behavior
Issue: #1620
1 parent b89aa48 commit f510e82

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

junit-jupiter-api/src/main/java/org/junit/jupiter/api/BeforeAll.java

+19-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
* methods may optionally declare parameters to be resolved by
3939
* {@link org.junit.jupiter.api.extension.ParameterResolver ParameterResolvers}.
4040
*
41-
* <h3>Inheritance</h3>
41+
* <h3>Inheritance and Execution Order</h3>
4242
*
4343
* <p>{@code @BeforeAll} methods are inherited from superclasses as long as
4444
* they are not <em>hidden</em> or <em>overridden</em>. Furthermore,
@@ -50,6 +50,24 @@
5050
* and {@code @BeforeAll} methods from an interface will be executed before
5151
* {@code @BeforeAll} methods in the class that implements the interface.
5252
*
53+
* <p>JUnit Jupiter does not guarantee the execution order of multiple
54+
* {@code @BeforeAll} methods that are declared within a single test class or
55+
* test interface. While it may at times appear that these methods are invoked
56+
* in alphabetical order, they are in fact sorted using an algorithm that is
57+
* deterministic but intentionally non-obvious.
58+
*
59+
* <p>In addition, {@code @BeforeAll} methods are in no way linked to
60+
* {@code @AfterAll} methods, i.e. there are no guarantees with regard to their
61+
* wrapping behavior. For example, given two {@code @BeforeAll} methods
62+
* {@code createA()} and {@code createB()} as well as two {@code @AfterAll}
63+
* methods {@code destroyA()} and {@code destroyB()}, the order in which the
64+
* {@code @BeforeAll} methods are executed (e.g. {@code createA()} before
65+
* {@code createB()}) does not imply any order for the seemingly corresponding
66+
* {@code @AfterAll} methods: (e.g. {@code destroyA()} might be called before
67+
* <em>or</em> after {@code destroyB()}). Thus, the JUnit Team recommends that
68+
* developers declare at most one {@code @BeforeAll}/{@code @AfterAll} method
69+
* per test class/interface unless there are no dependencies between them.
70+
*
5371
* <h3>Composition</h3>
5472
*
5573
* <p>{@code @BeforeAll} may be used as a meta-annotation in order to create

0 commit comments

Comments
 (0)