Skip to content

Commit 066a001

Browse files
committed
Document that @BeforeAll/@afterall are unsupported in @nested test classes
Issue: #88
1 parent 5dbfb44 commit 066a001

File tree

4 files changed

+12
-14
lines changed

4 files changed

+12
-14
lines changed

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

+2-6
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ module.
2525
|*`@AfterEach`* | Denotes that the annotated method should be executed _after_ *each* `@Test` method in the current class or class hierarchy
2626
|*`@BeforeAll`* | Denotes that the annotated method should be executed _before_ *all* `@Test` methods in the current class or class hierarchy; analogous to JUnit 4's `@BeforeClass`. Such methods must be `static`.
2727
|*`@AfterAll`* | Denotes that the annotated method should be executed _after_ *all* `@Test` methods in the current class or class hierarchy; analogous to JUnit 4's `@AfterClass`. Such methods must be `static`.
28-
|*`@Nested`* | Denotes that the annotated class is a nested test class.
28+
|*`@Nested`* | Denotes that the annotated class is a nested, non-static test class. Due to restrictions of the Java language, `@BeforeAll` and `@AfterAll` methods cannot be used in a `@Nested` test class.
2929
|*`@Tag`* and *`@Tags`* | Used to declare _tags_ for filtering tests, either at the class or method level; analogous to test groups in TestNG or Categories in JUnit 4
3030
|*`@Disabled`* | Used to _disable_ a test class or test method; analogous to JUnit 4's `@Ignore`
3131
|*`@ExtendWith`* | Used to register custom <<extension-model, extensions>> to the framework.
@@ -49,7 +49,6 @@ include::{testDir}/example/Fast.java[tags=user_guide]
4949
----
5050

5151

52-
5352
=== Standard Test Class
5453

5554
[source,java,indent=0]
@@ -59,10 +58,7 @@ include::{testDir}/example/Fast.java[tags=user_guide]
5958
include::{testDir}/example/StandardTests.java[tags=user_guide]
6059
----
6160

62-
63-
Notice that neither the test class nor the test method need to be `public`. `@BeforeAll`
64-
and `@AfterAll` must be static methods.
65-
61+
Notice that neither the test class nor the test method need to be `public`.
6662

6763

6864
=== Custom Names

junit5-api/src/main/java/org/junit/gen5/api/AfterAll.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@
2121
* executed <em>after</em> <strong>all</strong> tests in the current test
2222
* class or test class hierarchy.
2323
*
24-
* <p>In contrast to {@code @AfterEach} methods, {@code @AfterAll} methods
25-
* are only executed once for a given test class.
24+
* <p>In contrast to {@link AfterEach @AfterEach} methods, {@code @AfterAll}
25+
* methods are only executed once for a given test class.
2626
*
2727
* <p>{@code @AfterAll} methods must not be {@code private} and must be
28-
* {@code static}.
28+
* {@code static}. Consequently, {@code @AfterAll} methods are not
29+
* supported in {@link Nested @Nested} test classes.
2930
*
3031
* @since 5.0
3132
* @see BeforeAll

junit5-api/src/main/java/org/junit/gen5/api/BeforeAll.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@
2121
* executed <em>before</em> <strong>all</strong> tests in the current test
2222
* class or test class hierarchy.
2323
*
24-
* <p>In contrast to {@code @BeforeEach} methods, {@code @BeforeAll} methods
25-
* are only executed once for a given test class.
24+
* <p>In contrast to {@link BeforeEach @BeforeEach} methods, {@code @BeforeAll}
25+
* methods are only executed once for a given test class.
2626
*
2727
* <p>{@code @BeforeAll} methods must not be {@code private} and must be
28-
* {@code static}.
28+
* {@code static}. Consequently, {@code @BeforeAll} methods are not
29+
* supported in {@link Nested @Nested} test classes.
2930
*
3031
* @since 5.0
3132
* @see AfterAll

junit5-api/src/main/java/org/junit/gen5/api/Nested.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
import java.lang.annotation.Target;
1818

1919
/**
20-
* {@code @Nested} is used to signal that the annotated class is a nested
21-
* test class.
20+
* {@code @Nested} is used to signal that the annotated class is a nested,
21+
* non-static test class.
2222
*
2323
* @since 5.0
2424
*/

0 commit comments

Comments
 (0)