Skip to content

Commit fee9c4c

Browse files
committed
Simplify Nested lifecycle method docs due to JDK 17 requirement
1 parent f6efd0e commit fee9c4c

File tree

7 files changed

+19
-60
lines changed

7 files changed

+19
-60
lines changed

documentation/src/docs/asciidoc/user-guide/writing-tests.adoc

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,14 @@ overridden.
7979

8080
`*@BeforeAll*`:: Denotes that the annotated method should be executed _before_ *all*
8181
`@Test`, `@RepeatedTest`, `@ParameterizedTest`, and `@TestFactory` methods in the current
82-
class; analogous to JUnit 4's `@BeforeClass`. Such methods are inherited unless they are
83-
overridden and must be `static` unless the "per-class"
82+
top-level or `@Nested` test class; analogous to JUnit 4's `@BeforeClass`. Such methods are
83+
inherited unless they are overridden and must be `static` unless the "per-class"
8484
<<writing-tests-test-instance-lifecycle, test instance lifecycle>> is used.
8585

8686
`*@AfterAll*`:: Denotes that the annotated method should be executed _after_ *all*
8787
`@Test`, `@RepeatedTest`, `@ParameterizedTest`, and `@TestFactory` methods in the current
88-
class; analogous to JUnit 4's `@AfterClass`. Such methods are inherited unless they are
89-
overridden and must be `static` unless the "per-class"
88+
top-level or `@Nested` test class; analogous to JUnit 4's `@AfterClass`. Such methods are
89+
inherited unless they are overridden and must be `static` unless the "per-class"
9090
<<writing-tests-test-instance-lifecycle, test instance lifecycle>> is used.
9191

9292
`*@ParameterizedClass*`:: Denotes that the annotated class is a
@@ -109,12 +109,7 @@ multiple times depending on the number of invocation contexts returned by the re
109109
<<extensions-class-templates, providers>>. Such annotations are inherited.
110110

111111
`*@Nested*`:: Denotes that the annotated class is a non-static
112-
<<writing-tests-nested, nested test class>>. On Java 8 through Java 15, `@BeforeAll` and
113-
`@AfterAll` methods cannot be used directly in a `@Nested` test class unless the
114-
"per-class" <<writing-tests-test-instance-lifecycle, test instance lifecycle>> is used.
115-
Beginning with Java 16, `@BeforeAll` and `@AfterAll` methods can be declared as `static`
116-
in a `@Nested` test class with either test instance lifecycle mode. Such annotations are
117-
not inherited.
112+
<<writing-tests-nested, nested test class>>. Such annotations are not inherited.
118113

119114
`*@Tag*`:: Used to declare
120115
<<writing-tests-tagging-and-filtering, tags for filtering tests>>, either at the class or
@@ -1137,12 +1132,7 @@ methods rely on state stored in instance variables, you may need to reset that s
11371132

11381133
The "per-class" mode has some additional benefits over the default "per-method" mode.
11391134
Specifically, with the "per-class" mode it becomes possible to declare `@BeforeAll` and
1140-
`@AfterAll` on non-static methods as well as on interface `default` methods. The
1141-
"per-class" mode therefore also makes it possible to use `@BeforeAll` and `@AfterAll`
1142-
methods in `@Nested` test classes.
1143-
1144-
NOTE: Beginning with Java 16, `@BeforeAll` and `@AfterAll` methods can be declared as
1145-
`static` in `@Nested` test classes.
1135+
`@AfterAll` on non-static methods as well as on interface `default` methods.
11461136

11471137
If you are authoring tests using the Kotlin programming language, you may also find it
11481138
easier to implement non-static `@BeforeAll` and `@AfterAll` lifecycle methods as well as
@@ -1215,13 +1205,7 @@ running the outer tests, because the setup code from the outer tests is always e
12151205

12161206
NOTE: _Only non-static nested classes_ (i.e. _inner classes_) can serve as `@Nested` test
12171207
classes. Nesting can be arbitrarily deep, and those inner classes are subject to full
1218-
lifecycle support with one exception: `@BeforeAll` and `@AfterAll` methods do not work _by
1219-
default_. The reason is that Java does not allow `static` members in inner classes prior
1220-
to Java 16. However, this restriction can be circumvented by annotating a `@Nested` test
1221-
class with `@TestInstance(Lifecycle.PER_CLASS)` (see
1222-
<<writing-tests-test-instance-lifecycle>>). If you are using Java 16 or higher,
1223-
`@BeforeAll` and `@AfterAll` methods can be declared as `static` in `@Nested` test
1224-
classes, and this restriction no longer applies.
1208+
lifecycle support, including `@BeforeAll` and `@AfterAll` methods on each level.
12251209

12261210
[[writing-tests-nested-interoperability]]
12271211
==== Interoperability

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,9 @@
3636
* <h2>Method Signatures</h2>
3737
*
3838
* <p>{@code @AfterAll} methods must have a {@code void} return type and must
39-
* be {@code static} by default. Consequently, {@code @AfterAll} methods are
40-
* not supported in {@link Nested @Nested} test classes or as <em>interface
41-
* default methods</em> unless the test class is annotated with
42-
* {@link TestInstance @TestInstance(Lifecycle.PER_CLASS)}. However, beginning
43-
* with Java 16 {@code @AfterAll} methods may be declared as {@code static} in
44-
* {@link Nested @Nested} test classes, in which case the {@code Lifecycle.PER_CLASS}
45-
* restriction no longer applies. In addition, {@code @AfterAll} methods may
46-
* optionally declare parameters to be resolved by
39+
* be {@code static} unless the test class is annotated with
40+
* {@link TestInstance @TestInstance(Lifecycle.PER_CLASS)}. In addition,
41+
* {@code @AfterAll} methods may optionally declare parameters to be resolved by
4742
* {@link org.junit.jupiter.api.extension.ParameterResolver ParameterResolvers}.
4843
*
4944
* <p>Using {@code private} visibility for {@code @AfterAll} methods is strongly

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,9 @@
3636
* <h2>Method Signatures</h2>
3737
*
3838
* <p>{@code @BeforeAll} methods must have a {@code void} return type and must
39-
* be {@code static} by default. Consequently, {@code @BeforeAll} methods are
40-
* not supported in {@link Nested @Nested} test classes or as <em>interface
41-
* default methods</em> unless the test class is annotated with
42-
* {@link TestInstance @TestInstance(Lifecycle.PER_CLASS)}. However, beginning
43-
* with Java 16 {@code @BeforeAll} methods may be declared as {@code static} in
44-
* {@link Nested @Nested} test classes, in which case the {@code Lifecycle.PER_CLASS}
45-
* restriction no longer applies. In addition, {@code @BeforeAll} methods may
46-
* optionally declare parameters to be resolved by
39+
* be {@code static} unless the test class is annotated with
40+
* {@link TestInstance @TestInstance(Lifecycle.PER_CLASS)}. In addition,
41+
* {@code @BeforeAll} methods may optionally declare parameters to be resolved by
4742
* {@link org.junit.jupiter.api.extension.ParameterResolver ParameterResolvers}.
4843
*
4944
* <p>Using {@code private} visibility for {@code @BeforeAll} methods is strongly

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,7 @@
4646
* as well as between non-static {@link BeforeAll @BeforeAll} and
4747
* {@link AfterAll @AfterAll} methods in the test class.</li>
4848
* <li>Declaration of non-static {@code @BeforeAll} and {@code @AfterAll} methods
49-
* in {@link Nested @Nested} test classes. Beginning with Java 16, {@code @BeforeAll}
50-
* and {@code @AfterAll} methods may be declared as {@code static} in
51-
* {@link Nested @Nested} test classes with either lifecycle mode.</li>
49+
* in top-level or {@link Nested @Nested} test classes.</li>
5250
* <li>Declaration of {@code @BeforeAll} and {@code @AfterAll} on interface
5351
* {@code default} methods.</li>
5452
* <li>Simplified declaration of non-static {@code @BeforeAll} and {@code @AfterAll}

junit-jupiter-params/src/main/java/org/junit/jupiter/params/AfterParameterizedClassInvocation.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,9 @@
3434
* <h2>Method Signatures</h2>
3535
*
3636
* <p>{@code @AfterParameterizedClassInvocation} methods must have a
37-
* {@code void} return type, must not be private, and must be {@code static} by
38-
* default. Consequently, {@code @AfterParameterizedClassInvocation} methods are
39-
* not supported in {@link org.junit.jupiter.api.Nested @Nested} test classes or
40-
* as <em>interface default methods</em> unless the test class is annotated with
37+
* {@code void} return type, must not be private, and must be {@code static}
38+
* unless the test class is annotated with
4139
* {@link org.junit.jupiter.api.TestInstance @TestInstance(Lifecycle.PER_CLASS)}.
42-
* However, beginning with Java 16 {@code @AfterParameterizedClassInvocation}
43-
* methods may be declared as {@code static} in
44-
* {@link org.junit.jupiter.api.Nested @Nested} test classes, in which case the
45-
* {@code Lifecycle.PER_CLASS} restriction no longer applies.
4640
*
4741
* <h2>Method Arguments</h2>
4842
*

junit-jupiter-params/src/main/java/org/junit/jupiter/params/BeforeParameterizedClassInvocation.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,9 @@
3434
* <h2>Method Signatures</h2>
3535
*
3636
* <p>{@code @BeforeParameterizedClassInvocation} methods must have a
37-
* {@code void} return type, must not be private, and must be {@code static} by
38-
* default. Consequently, {@code @BeforeParameterizedClassInvocation} methods
39-
* are not supported in {@link org.junit.jupiter.api.Nested @Nested} test
40-
* classes or as <em>interface default methods</em> unless the test class is
41-
* annotated with
37+
* {@code void} return type, must not be private, and must be {@code static}
38+
* unless the test class is annotated with
4239
* {@link org.junit.jupiter.api.TestInstance @TestInstance(Lifecycle.PER_CLASS)}.
43-
* However, beginning with Java 16 {@code @BeforeParameterizedClassInvocation}
44-
* methods may be declared as {@code static} in
45-
* {@link org.junit.jupiter.api.Nested @Nested} test classes, in which case the
46-
* {@code Lifecycle.PER_CLASS} restriction no longer applies.
4740
*
4841
* <h2>Method Arguments</h2>
4942
*

jupiter-tests/src/test/java/org/junit/jupiter/engine/StaticNestedBeforeAllAndAfterAllMethodsTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
/**
2727
* Integration tests that verify support for {@code static} {@link BeforeAll} and
28-
* {@link AfterAll} methods in {@link Nested} tests on Java 16+.
28+
* {@link AfterAll} methods in {@link Nested} tests.
2929
*
3030
* @since 5.9
3131
* @see BeforeAllAndAfterAllComposedAnnotationTests

0 commit comments

Comments
 (0)