Skip to content

Commit ee34052

Browse files
committed
Revert disallowing private lifecycle methods
Document using private as being strongly discouraged. Resolves #3242. (cherry picked from commit 594f7e4)
1 parent 5427a9b commit ee34052

File tree

7 files changed

+30
-99
lines changed

7 files changed

+30
-99
lines changed

Diff for: documentation/src/docs/asciidoc/release-notes/release-notes-5.9.3.adoc

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ JUnit repository on GitHub.
3333

3434
* Exceptions thrown for undeletable files when cleaning up a temporary directory created
3535
via `@TempDir` now include the root cause.
36+
* Allow lifecycle methods to be declared as `private` again for backwards compatibility
37+
but document it as a discouraged practice.
3638

3739
==== Deprecations and Breaking Changes
3840

Diff for: junit-jupiter-api/src/main/java/org/junit/jupiter/api/AfterAll.java

+8-5
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,20 @@
2929
*
3030
* <h2>Method Signatures</h2>
3131
*
32-
* <p>{@code @AfterAll} methods must have a {@code void} return type, must not
33-
* be {@code private}, and must be {@code static} by default. Consequently,
34-
* {@code @AfterAll} methods are not supported in {@link Nested @Nested} test
35-
* classes or as <em>interface default methods</em> unless the test class is
36-
* annotated with {@link TestInstance @TestInstance(Lifecycle.PER_CLASS)}.
32+
* <p>{@code @AfterAll} methods must have a {@code void} return type and must be
33+
* {@code static} by default. Consequently, {@code @AfterAll} methods are not
34+
* supported in {@link Nested @Nested} test classes or as <em>interface default
35+
* methods</em> unless the test class is annotated with
36+
* {@link TestInstance @TestInstance(Lifecycle.PER_CLASS)}.
3737
* However, beginning with Java 16 {@code @AfterAll} methods may be declared as
3838
* {@code static} in {@link Nested @Nested} test classes, and the
3939
* {@code Lifecycle.PER_CLASS} restriction no longer applies. {@code @AfterAll}
4040
* methods may optionally declare parameters to be resolved by
4141
* {@link org.junit.jupiter.api.extension.ParameterResolver ParameterResolvers}.
4242
*
43+
* <p>Using {@code private} visibility for {@code @BeforeAll} methods is
44+
* strongly discouraged and will be disallowed in a future release.
45+
*
4346
* <h2>Inheritance and Execution Order</h2>
4447
*
4548
* <p>{@code @AfterAll} methods are inherited from superclasses as long as

Diff for: junit-jupiter-api/src/main/java/org/junit/jupiter/api/AfterEach.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@
2828
*
2929
* <h2>Method Signatures</h2>
3030
*
31-
* <p>{@code @AfterEach} methods must have a {@code void} return type,
32-
* must not be {@code private}, and must not be {@code static}.
31+
* <p>{@code @AfterEach} methods must have a {@code void} return type and must
32+
* not be {@code static}. Using {@code private} visibility is strongly
33+
* discouraged and will be disallowed in a future release.
3334
* They may optionally declare parameters to be resolved by
3435
* {@link org.junit.jupiter.api.extension.ParameterResolver ParameterResolvers}.
3536
*

Diff for: junit-jupiter-api/src/main/java/org/junit/jupiter/api/BeforeAll.java

+8-5
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,20 @@
2929
*
3030
* <h2>Method Signatures</h2>
3131
*
32-
* <p>{@code @BeforeAll} methods must have a {@code void} return type, must not
33-
* be {@code private}, and must be {@code static} by default. Consequently,
34-
* {@code @BeforeAll} methods are not supported in {@link Nested @Nested} test
35-
* classes or as <em>interface default methods</em> unless the test class is
36-
* annotated with {@link TestInstance @TestInstance(Lifecycle.PER_CLASS)}.
32+
* <p>{@code @BeforeAll} methods must have a {@code void} return type and must
33+
* be {@code static} by default. Consequently, {@code @BeforeAll} methods are
34+
* not supported in {@link Nested @Nested} test classes or as <em>interface
35+
* default methods</em> unless the test class is annotated with
36+
* {@link TestInstance @TestInstance(Lifecycle.PER_CLASS)}.
3737
* However, beginning with Java 16 {@code @BeforeAll} methods may be declared as
3838
* {@code static} in {@link Nested @Nested} test classes, and the
3939
* {@code Lifecycle.PER_CLASS} restriction no longer applies. {@code @BeforeAll}
4040
* methods may optionally declare parameters to be resolved by
4141
* {@link org.junit.jupiter.api.extension.ParameterResolver ParameterResolvers}.
4242
*
43+
* <p>Using {@code private} visibility for {@code @BeforeAll} methods is
44+
* strongly discouraged and will be disallowed in a future release.
45+
*
4346
* <h2>Inheritance and Execution Order</h2>
4447
*
4548
* <p>{@code @BeforeAll} methods are inherited from superclasses as long as

Diff for: junit-jupiter-api/src/main/java/org/junit/jupiter/api/BeforeEach.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@
2828
*
2929
* <h2>Method Signatures</h2>
3030
*
31-
* <p>{@code @BeforeEach} methods must have a {@code void} return type,
32-
* must not be {@code private}, and must not be {@code static}.
31+
* <p>{@code @BeforeEach} methods must have a {@code void} return type and must
32+
* not be {@code static}. Using {@code private} visibility is strongly
33+
* discouraged and will be disallowed in a future release.
3334
* They may optionally declare parameters to be resolved by
3435
* {@link org.junit.jupiter.api.extension.ParameterResolver ParameterResolvers}.
3536
*

Diff for: junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/LifecycleMethodUtils.java

+1-12
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,14 @@ private static List<Method> findMethodsAndAssertStaticAndNonPrivate(Class<?> tes
6161
if (requireStatic) {
6262
methods.forEach(method -> assertStatic(annotationType, method));
6363
}
64-
methods.forEach(method -> assertNonPrivate(annotationType, method));
6564
return methods;
6665
}
6766

6867
private static List<Method> findMethodsAndAssertNonStaticAndNonPrivate(Class<?> testClass,
6968
Class<? extends Annotation> annotationType, HierarchyTraversalMode traversalMode) {
7069

7170
List<Method> methods = findMethodsAndCheckVoidReturnType(testClass, annotationType, traversalMode);
72-
methods.forEach(method -> {
73-
assertNonStatic(annotationType, method);
74-
assertNonPrivate(annotationType, method);
75-
});
71+
methods.forEach(method -> assertNonStatic(annotationType, method));
7672
return methods;
7773
}
7874

@@ -99,13 +95,6 @@ private static void assertNonStatic(Class<? extends Annotation> annotationType,
9995
}
10096
}
10197

102-
private static void assertNonPrivate(Class<? extends Annotation> annotationType, Method method) {
103-
if (ReflectionUtils.isPrivate(method)) {
104-
throw new JUnitException(String.format("@%s method '%s' must not be private.",
105-
annotationType.getSimpleName(), method.toGenericString()));
106-
}
107-
}
108-
10998
private static void assertVoid(Class<? extends Annotation> annotationType, Method method) {
11099
if (!returnsVoid(method)) {
111100
throw new JUnitException(String.format("@%s method '%s' must not return a value.",

Diff for: junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/InvalidLifecycleMethodConfigurationTests.java

+5-73
Original file line numberDiff line numberDiff line change
@@ -35,45 +35,25 @@ class InvalidLifecycleMethodConfigurationTests extends AbstractJupiterTestEngine
3535

3636
@Test
3737
void executeValidTestCaseAlongsideTestCaseWithInvalidNonStaticBeforeAllDeclaration() {
38-
assertExecutionResults(TestCaseWithInvalidNonStaticBeforeAllMethod.class);
39-
}
40-
41-
@Test
42-
void executeValidTestCaseAlongsideTestCaseWithInvalidPrivateBeforeAllDeclaration() {
43-
assertExecutionResults(TestCaseWithInvalidPrivateBeforeAllMethod.class);
38+
assertContainerFailed(TestCaseWithInvalidNonStaticBeforeAllMethod.class);
4439
}
4540

4641
@Test
4742
void executeValidTestCaseAlongsideTestCaseWithInvalidNonStaticAfterAllDeclaration() {
48-
assertExecutionResults(TestCaseWithInvalidNonStaticAfterAllMethod.class);
49-
}
50-
51-
@Test
52-
void executeValidTestCaseAlongsideTestCaseWithInvalidPrivateAfterAllDeclaration() {
53-
assertExecutionResults(TestCaseWithInvalidPrivateAfterAllMethod.class);
43+
assertContainerFailed(TestCaseWithInvalidNonStaticAfterAllMethod.class);
5444
}
5545

5646
@Test
5747
void executeValidTestCaseAlongsideTestCaseWithInvalidStaticBeforeEachDeclaration() {
58-
assertExecutionResults(TestCaseWithInvalidStaticBeforeEachMethod.class);
59-
}
60-
61-
@Test
62-
void executeValidTestCaseAlongsideTestCaseWithInvalidPrivateBeforeEachDeclaration() {
63-
assertExecutionResults(TestCaseWithInvalidPrivateBeforeEachMethod.class);
48+
assertContainerFailed(TestCaseWithInvalidStaticBeforeEachMethod.class);
6449
}
6550

6651
@Test
6752
void executeValidTestCaseAlongsideTestCaseWithInvalidStaticAfterEachDeclaration() {
68-
assertExecutionResults(TestCaseWithInvalidStaticAfterEachMethod.class);
69-
}
70-
71-
@Test
72-
void executeValidTestCaseAlongsideTestCaseWithInvalidPrivateAfterEachDeclaration() {
73-
assertExecutionResults(TestCaseWithInvalidPrivateAfterEachMethod.class);
53+
assertContainerFailed(TestCaseWithInvalidStaticAfterEachMethod.class);
7454
}
7555

76-
private void assertExecutionResults(Class<?> invalidTestClass) {
56+
private void assertContainerFailed(Class<?> invalidTestClass) {
7757
EngineExecutionResults executionResults = executeTests(selectClass(TestCase.class),
7858
selectClass(invalidTestClass));
7959
Events containers = executionResults.containerEvents();
@@ -112,18 +92,6 @@ void test() {
11292
}
11393
}
11494

115-
static class TestCaseWithInvalidPrivateBeforeAllMethod {
116-
117-
// must not be private
118-
@BeforeAll
119-
private static void beforeAll() {
120-
}
121-
122-
@Test
123-
void test() {
124-
}
125-
}
126-
12795
static class TestCaseWithInvalidNonStaticAfterAllMethod {
12896

12997
// must be static
@@ -136,18 +104,6 @@ void test() {
136104
}
137105
}
138106

139-
static class TestCaseWithInvalidPrivateAfterAllMethod {
140-
141-
// must not be private
142-
@AfterAll
143-
private static void afterAll() {
144-
}
145-
146-
@Test
147-
void test() {
148-
}
149-
}
150-
151107
static class TestCaseWithInvalidStaticBeforeEachMethod {
152108

153109
// must NOT be static
@@ -160,18 +116,6 @@ void test() {
160116
}
161117
}
162118

163-
static class TestCaseWithInvalidPrivateBeforeEachMethod {
164-
165-
// must NOT be private
166-
@BeforeEach
167-
private void beforeEach() {
168-
}
169-
170-
@Test
171-
void test() {
172-
}
173-
}
174-
175119
static class TestCaseWithInvalidStaticAfterEachMethod {
176120

177121
// must NOT be static
@@ -184,16 +128,4 @@ void test() {
184128
}
185129
}
186130

187-
static class TestCaseWithInvalidPrivateAfterEachMethod {
188-
189-
// must NOT be private
190-
@AfterEach
191-
private void afterEach() {
192-
}
193-
194-
@Test
195-
void test() {
196-
}
197-
}
198-
199131
}

0 commit comments

Comments
 (0)