Skip to content

Commit 29c7ad9

Browse files
committed
Document execution order and wrapping behavior
Issue: #1620
1 parent 29673c6 commit 29c7ad9

File tree

4 files changed

+75
-11
lines changed

4 files changed

+75
-11
lines changed

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

+22-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 @AfterAll} methods are inherited from superclasses as long as
4444
* they are not <em>hidden</em> or <em>overridden</em>. Furthermore,
@@ -50,6 +50,27 @@
5050
* and {@code @AfterAll} methods from an interface will be executed after
5151
* {@code @AfterAll} methods in the class that implements the interface.
5252
*
53+
* <p>JUnit Jupiter does not guarantee the execution order of multiple
54+
* {@code @AfterAll} 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 @AfterAll} methods are in no way linked to
60+
* {@code @BeforeAll} methods. Consequently, there are no guarantees with regard
61+
* to their <em>wrapping</em> behavior. For example, given two
62+
* {@code @BeforeAll} methods {@code createA()} and {@code createB()} as well as
63+
* two {@code @AfterAll} methods {@code destroyA()} and {@code destroyB()}, the
64+
* order in which the {@code @BeforeAll} methods are executed (e.g.
65+
* {@code createA()} before {@code createB()}) does not imply any order for the
66+
* seemingly corresponding {@code @AfterAll} methods. In other words,
67+
* {@code destroyA()} might be called before <em>or</em> after
68+
* {@code destroyB()}. The JUnit Team therefore recommends that developers
69+
* declare at most one {@code @BeforeAll} method and at most one
70+
* {@code @AfterAll} method per test class or test interface unless there are no
71+
* dependencies between the {@code @BeforeAll} methods or between the
72+
* {@code @AfterAll} methods.
73+
*
5374
* <h3>Composition</h3>
5475
*
5576
* <p>{@code @AfterAll} may be used as a meta-annotation in order to create

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

+22-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
* They may optionally declare parameters to be resolved by
3434
* {@link org.junit.jupiter.api.extension.ParameterResolver ParameterResolvers}.
3535
*
36-
* <h3>Inheritance</h3>
36+
* <h3>Inheritance and Execution Order</h3>
3737
*
3838
* <p>{@code @AfterEach} methods are inherited from superclasses as long as
3939
* they are not overridden. Furthermore, {@code @AfterEach} methods from
@@ -44,6 +44,27 @@
4444
* {@code @AfterEach} default methods will be executed after {@code @AfterEach}
4545
* methods in the class that implements the interface.
4646
*
47+
* <p>JUnit Jupiter does not guarantee the execution order of multiple
48+
* {@code @AfterEach} methods that are declared within a single test class or
49+
* test interface. While it may at times appear that these methods are invoked
50+
* in alphabetical order, they are in fact sorted using an algorithm that is
51+
* deterministic but intentionally non-obvious.
52+
*
53+
* <p>In addition, {@code @AfterEach} methods are in no way linked to
54+
* {@code @BeforeEach} methods. Consequently, there are no guarantees with
55+
* regard to their <em>wrapping</em> behavior. For example, given two
56+
* {@code @BeforeEach} methods {@code createA()} and {@code createB()} as well
57+
* as two {@code @AfterEach} methods {@code destroyA()} and {@code destroyB()},
58+
* the order in which the {@code @BeforeEach} methods are executed (e.g.
59+
* {@code createA()} before {@code createB()}) does not imply any order for the
60+
* seemingly corresponding {@code @AfterEach} methods. In other words,
61+
* {@code destroyA()} might be called before <em>or</em> after
62+
* {@code destroyB()}. The JUnit Team therefore recommends that developers
63+
* declare at most one {@code @BeforeEach} method and at most one
64+
* {@code @AfterEach} method per test class or test interface unless there are
65+
* no dependencies between the {@code @BeforeEach} methods or between the
66+
* {@code @AfterEach} methods.
67+
*
4768
* <h3>Composition</h3>
4869
*
4970
* <p>{@code @AfterEach} may be used as a meta-annotation in order to create

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

+9-8
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,15 @@
5858
*
5959
* <p>In addition, {@code @BeforeAll} methods are in no way linked to
6060
* {@code @AfterAll} methods. Consequently, there are no guarantees with regard
61-
* to their <em>wrapping</em> behavior. For example, given two {@code @BeforeAll}
62-
* methods {@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. In other words, {@code destroyA()} might be called
67-
* before <em>or</em> after {@code destroyB()}. The JUnit Team therefore recommends
68-
* that developers declare at most one {@code @BeforeAll} method and at most one
61+
* to their <em>wrapping</em> behavior. For example, given two
62+
* {@code @BeforeAll} methods {@code createA()} and {@code createB()} as well as
63+
* two {@code @AfterAll} methods {@code destroyA()} and {@code destroyB()}, the
64+
* order in which the {@code @BeforeAll} methods are executed (e.g.
65+
* {@code createA()} before {@code createB()}) does not imply any order for the
66+
* seemingly corresponding {@code @AfterAll} methods. In other words,
67+
* {@code destroyA()} might be called before <em>or</em> after
68+
* {@code destroyB()}. The JUnit Team therefore recommends that developers
69+
* declare at most one {@code @BeforeAll} method and at most one
6970
* {@code @AfterAll} method per test class or test interface unless there are no
7071
* dependencies between the {@code @BeforeAll} methods or between the
7172
* {@code @AfterAll} methods.

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

+22-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
* They may optionally declare parameters to be resolved by
3434
* {@link org.junit.jupiter.api.extension.ParameterResolver ParameterResolvers}.
3535
*
36-
* <h3>Inheritance</h3>
36+
* <h3>Inheritance and Execution Order</h3>
3737
*
3838
* <p>{@code @BeforeEach} methods are inherited from superclasses as long as
3939
* they are not overridden. Furthermore, {@code @BeforeEach} methods from
@@ -44,6 +44,27 @@
4444
* {@code @BeforeEach} default methods will be executed before {@code @BeforeEach}
4545
* methods in the class that implements the interface.
4646
*
47+
* <p>JUnit Jupiter does not guarantee the execution order of multiple
48+
* {@code @BeforeEach} methods that are declared within a single test class or
49+
* test interface. While it may at times appear that these methods are invoked
50+
* in alphabetical order, they are in fact sorted using an algorithm that is
51+
* deterministic but intentionally non-obvious.
52+
*
53+
* <p>In addition, {@code @BeforeEach} methods are in no way linked to
54+
* {@code @AfterEach} methods. Consequently, there are no guarantees with regard
55+
* to their <em>wrapping</em> behavior. For example, given two
56+
* {@code @BeforeEach} methods {@code createA()} and {@code createB()} as well
57+
* as two {@code @AfterEach} methods {@code destroyA()} and {@code destroyB()},
58+
* the order in which the {@code @BeforeEach} methods are executed (e.g.
59+
* {@code createA()} before {@code createB()}) does not imply any order for the
60+
* seemingly corresponding {@code @AfterEach} methods. In other words,
61+
* {@code destroyA()} might be called before <em>or</em> after
62+
* {@code destroyB()}. The JUnit Team therefore recommends that developers
63+
* declare at most one {@code @BeforeEach} method and at most one
64+
* {@code @AfterEach} method per test class or test interface unless there are
65+
* no dependencies between the {@code @BeforeEach} methods or between the
66+
* {@code @AfterEach} methods.
67+
*
4768
* <h3>Composition</h3>
4869
*
4970
* <p>{@code @BeforeEach} may be used as a meta-annotation in order to create

0 commit comments

Comments
 (0)