Skip to content

Commit f5485fa

Browse files
committed
Polishing
1 parent 0a2c3e5 commit f5485fa

File tree

10 files changed

+134
-51
lines changed

10 files changed

+134
-51
lines changed

documentation/src/docs/asciidoc/release-notes/release-notes-5.9.0-RC1.adoc

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ on GitHub.
3030
[[release-notes-5.9.0-RC1-junit-jupiter]]
3131
=== JUnit Jupiter
3232

33+
==== Deprecations and Breaking Changes
34+
35+
* Private lifecycle methods (annotated with `@BeforeAll`, `@AfterAll`, `@BeforeEach`, or
36+
`@AfterEach`) now correctly lead to an exception. Although this is a bug fix, it is
37+
technically also a breaking change since there might be existing user code with
38+
`private` lifecycle methods which will now start to fail.
39+
3340
==== New Features and Improvements
3441

3542
* Three new `abort` methods have been added to the `Assumptions` class. These are
@@ -48,11 +55,6 @@ on GitHub.
4855
executes the test code in the same thread as the calling code, and `SEPARATE_THREAD`
4956
which executes it in a separate thread.
5057

51-
==== Deprecations and Breaking Changes
52-
53-
* Private lifecycle methods now correctly lead to an exception. Despite being a bug fix,
54-
this is technically a breaking change as there might be existing user code with private
55-
lifecycle methods which will now start to fail.
5658

5759
[[release-notes-5.9.0-RC1-junit-vintage]]
5860
=== JUnit Vintage

documentation/src/docs/asciidoc/user-guide/extensions.adoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -799,14 +799,14 @@ callbacks implemented by `Extension2`. `Extension1` is therefore said to _wrap_
799799
JUnit Jupiter also guarantees _wrapping_ behavior within class and interface hierarchies
800800
for user-supplied _lifecycle methods_ (see <<writing-tests-classes-and-methods>>).
801801

802-
* `@BeforeAll` methods are inherited from superclasses as long as they are not _hidden_, or
802+
* `@BeforeAll` methods are inherited from superclasses as long as they are not _hidden_,
803803
_overridden_, or _superseded_ (i.e., replaced based on signature only, irrespective of
804804
Java's visibility rules). Furthermore, `@BeforeAll` methods from superclasses will be
805805
executed **before** `@BeforeAll` methods in subclasses.
806806
** Similarly, `@BeforeAll` methods declared in an interface are inherited as long as they
807807
are not _hidden_ or _overridden_, and `@BeforeAll` methods from an interface will be
808808
executed **before** `@BeforeAll` methods in the class that implements the interface.
809-
* `@AfterAll` methods are inherited from superclasses as long as they are not _hidden_, or
809+
* `@AfterAll` methods are inherited from superclasses as long as they are not _hidden_,
810810
_overridden_, or _superseded_ (i.e., replaced based on signature only, irrespective of
811811
Java's visibility rules). Furthermore, `@AfterAll` methods from superclasses will be
812812
executed **after** `@AfterAll` methods in subclasses.

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ in the `junit-jupiter-api` module.
3434
| `@TestInstance` | Used to configure the <<writing-tests-test-instance-lifecycle, test instance lifecycle>> for the annotated test class. Such annotations are _inherited_.
3535
| `@DisplayName` | Declares a custom <<writing-tests-display-names,display name>> for the test class or test method. Such annotations are not _inherited_.
3636
| `@DisplayNameGeneration` | Declares a custom <<writing-tests-display-name-generator,display name generator>> for the test class. Such annotations are _inherited_.
37-
| `@BeforeEach` | Denotes that the annotated method should be executed _before_ *each* `@Test`, `@RepeatedTest`, `@ParameterizedTest`, or `@TestFactory` method in the current class; analogous to JUnit 4's `@Before`. Such methods are _inherited_ unless they are _overridden_ or _superseded_ (i.e., replaced based on signature only, irrespective of Java's visibility rules).
38-
| `@AfterEach` | Denotes that the annotated method should be executed _after_ *each* `@Test`, `@RepeatedTest`, `@ParameterizedTest`, or `@TestFactory` method in the current class; analogous to JUnit 4's `@After`. Such methods are _inherited_ unless they are _overridden_ or _superseded_ (i.e., replaced based on signature only, irrespective of Java's visibility rules).
39-
| `@BeforeAll` | Denotes that the annotated method should be executed _before_ *all* `@Test`, `@RepeatedTest`, `@ParameterizedTest`, and `@TestFactory` methods in the current class; analogous to JUnit 4's `@BeforeClass`. Such methods are _inherited_ (unless they are _hidden_, or _overridden_, or _superseded_, i.e., replaced based on signature only, irrespective of Java's visibility rules) and must be `static` (unless the "per-class" <<writing-tests-test-instance-lifecycle, test instance lifecycle>> is used).
40-
| `@AfterAll` | Denotes that the annotated method should be executed _after_ *all* `@Test`, `@RepeatedTest`, `@ParameterizedTest`, and `@TestFactory` methods in the current class; analogous to JUnit 4's `@AfterClass`. Such methods are _inherited_ (unless they are _hidden_, or _overridden_, or _superseded_, i.e., replaced based on signature only, irrespective of Java's visibility rules) and must be `static` (unless the "per-class" <<writing-tests-test-instance-lifecycle, test instance lifecycle>> is used).
37+
| `@BeforeEach` | Denotes that the annotated method should be executed _before_ *each* `@Test`, `@RepeatedTest`, `@ParameterizedTest`, or `@TestFactory` method in the current class; analogous to JUnit 4's `@Before`. Such methods are _inherited_ unless they are _overridden_ or _superseded_ (i.e., replaced based on signature only, irrespective of Java's visibility rules).
38+
| `@AfterEach` | Denotes that the annotated method should be executed _after_ *each* `@Test`, `@RepeatedTest`, `@ParameterizedTest`, or `@TestFactory` method in the current class; analogous to JUnit 4's `@After`. Such methods are _inherited_ unless they are _overridden_ or _superseded_ (i.e., replaced based on signature only, irrespective of Java's visibility rules).
39+
| `@BeforeAll` | Denotes that the annotated method should be executed _before_ *all* `@Test`, `@RepeatedTest`, `@ParameterizedTest`, and `@TestFactory` methods in the current class; analogous to JUnit 4's `@BeforeClass`. Such methods are _inherited_ unless they are _hidden_, _overridden_, or _superseded_, (i.e., replaced based on signature only, irrespective of Java's visibility rules) and must be `static` unless the "per-class" <<writing-tests-test-instance-lifecycle, test instance lifecycle>> is used.
40+
| `@AfterAll` | Denotes that the annotated method should be executed _after_ *all* `@Test`, `@RepeatedTest`, `@ParameterizedTest`, and `@TestFactory` methods in the current class; analogous to JUnit 4's `@AfterClass`. Such methods are _inherited_ unless they are _hidden_, _overridden_, or _superseded_, (i.e., replaced based on signature only, irrespective of Java's visibility rules) and must be `static` unless the "per-class" <<writing-tests-test-instance-lifecycle, test instance lifecycle>> is used.
4141
| `@Nested` | Denotes that the annotated class is a non-static <<writing-tests-nested,nested test class>>. On Java 8 through Java 15, `@BeforeAll` and `@AfterAll` methods cannot be used directly in a `@Nested` test class unless the "per-class" <<writing-tests-test-instance-lifecycle, test instance lifecycle>> is used. Beginning with Java 16, `@BeforeAll` and `@AfterAll` methods can be declared as `static` in a `@Nested` test class with either test instance lifecycle mode. Such annotations are not _inherited_.
4242
| `@Tag` | Used to declare <<writing-tests-tagging-and-filtering,tags for filtering tests>>, either at the class or method level; analogous to test groups in TestNG or Categories in JUnit 4. Such annotations are _inherited_ at the class level but not at the method level.
4343
| `@Disabled` | Used to <<writing-tests-disabling,disable>> a test class or test method; analogous to JUnit 4's `@Ignore`. Such annotations are not _inherited_.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
* <h2>Inheritance and Execution Order</h2>
4444
*
4545
* <p>{@code @AfterAll} methods are inherited from superclasses as long as
46-
* they are not <em>hidden</em> (default mode with {@code static} modifier), or
46+
* they are not <em>hidden</em> (default mode with {@code static} modifier),
4747
* <em>overridden</em>, or <em>superseded</em> (i.e., replaced based on
4848
* signature only, irrespective of Java's visibility rules). Furthermore,
4949
* {@code @AfterAll} methods from superclasses will be executed before

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535
*
3636
* <h2>Inheritance and Execution Order</h2>
3737
*
38-
* <p>{@code @AfterEach} methods are inherited from superclasses as long as
39-
* they are not overridden or <em>superseded</em> (i.e., replaced based on
38+
* <p>{@code @AfterEach} methods are inherited from superclasses as long as they
39+
* are not <em>overridden</em> or <em>superseded</em> (i.e., replaced based on
4040
* signature only, irrespective of Java's visibility rules). Furthermore,
4141
* {@code @AfterEach} methods from superclasses will be executed after
4242
* {@code @AfterEach} methods in subclasses.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
* <h2>Inheritance and Execution Order</h2>
4444
*
4545
* <p>{@code @BeforeAll} methods are inherited from superclasses as long as
46-
* they are not <em>hidden</em> (default mode with {@code static} modifier), or
46+
* they are not <em>hidden</em> (default mode with {@code static} modifier),
4747
* <em>overridden</em>, or <em>superseded</em> (i.e., replaced based on
4848
* signature only, irrespective of Java's visibility rules). Furthermore,
4949
* {@code @BeforeAll} methods from superclasses will be executed before

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535
*
3636
* <h2>Inheritance and Execution Order</h2>
3737
*
38-
* <p>{@code @BeforeEach} methods are inherited from superclasses as long as
39-
* they are not overridden or <em>superseded</em> (i.e., replaced based on
38+
* <p>{@code @BeforeEach} methods are inherited from superclasses as long as they
39+
* are not <em>overridden</em> or <em>superseded</em> (i.e., replaced based on
4040
* signature only, irrespective of Java's visibility rules). Furthermore,
4141
* {@code @BeforeEach} methods from superclasses will be executed before
4242
* {@code @BeforeEach} methods in subclasses.

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

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,36 @@ static List<Method> findAfterEachMethods(Class<?> testClass) {
5454
return findMethodsAndAssertNonStaticAndNonPrivate(testClass, AfterEach.class, HierarchyTraversalMode.BOTTOM_UP);
5555
}
5656

57+
private static List<Method> findMethodsAndAssertStaticAndNonPrivate(Class<?> testClass, boolean requireStatic,
58+
Class<? extends Annotation> annotationType, HierarchyTraversalMode traversalMode) {
59+
60+
List<Method> methods = findMethodsAndCheckVoidReturnType(testClass, annotationType, traversalMode);
61+
if (requireStatic) {
62+
methods.forEach(method -> assertStatic(annotationType, method));
63+
}
64+
methods.forEach(method -> assertNonPrivate(annotationType, method));
65+
return methods;
66+
}
67+
68+
private static List<Method> findMethodsAndAssertNonStaticAndNonPrivate(Class<?> testClass,
69+
Class<? extends Annotation> annotationType, HierarchyTraversalMode traversalMode) {
70+
71+
List<Method> methods = findMethodsAndCheckVoidReturnType(testClass, annotationType, traversalMode);
72+
methods.forEach(method -> {
73+
assertNonStatic(annotationType, method);
74+
assertNonPrivate(annotationType, method);
75+
});
76+
return methods;
77+
}
78+
79+
private static List<Method> findMethodsAndCheckVoidReturnType(Class<?> testClass,
80+
Class<? extends Annotation> annotationType, HierarchyTraversalMode traversalMode) {
81+
82+
List<Method> methods = findAnnotatedMethods(testClass, annotationType, traversalMode);
83+
methods.forEach(method -> assertVoid(annotationType, method));
84+
return methods;
85+
}
86+
5787
private static void assertStatic(Class<? extends Annotation> annotationType, Method method) {
5888
if (ReflectionUtils.isNotStatic(method)) {
5989
throw new JUnitException(String.format(
@@ -83,33 +113,4 @@ private static void assertVoid(Class<? extends Annotation> annotationType, Metho
83113
}
84114
}
85115

86-
private static List<Method> findMethodsAndAssertStaticAndNonPrivate(Class<?> testClass, boolean requireStatic,
87-
Class<? extends Annotation> annotationType, HierarchyTraversalMode traversalMode) {
88-
List<Method> methods = findMethodsAndCheckVoidReturnType(testClass, annotationType, traversalMode);
89-
if (requireStatic) {
90-
methods.forEach(method -> assertStatic(annotationType, method));
91-
}
92-
methods.forEach(method -> assertNonPrivate(annotationType, method));
93-
94-
return methods;
95-
}
96-
97-
private static List<Method> findMethodsAndAssertNonStaticAndNonPrivate(Class<?> testClass,
98-
Class<? extends Annotation> annotationType, HierarchyTraversalMode traversalMode) {
99-
List<Method> methods = findMethodsAndCheckVoidReturnType(testClass, annotationType, traversalMode);
100-
methods.forEach(method -> {
101-
assertNonStatic(annotationType, method);
102-
assertNonPrivate(annotationType, method);
103-
});
104-
105-
return methods;
106-
}
107-
108-
private static List<Method> findMethodsAndCheckVoidReturnType(Class<?> testClass,
109-
Class<? extends Annotation> annotationType, HierarchyTraversalMode traversalMode) {
110-
List<Method> methods = findAnnotatedMethods(testClass, annotationType, traversalMode);
111-
methods.forEach(method -> assertVoid(annotationType, method));
112-
return methods;
113-
}
114-
115116
}

junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/LifecycleMethodSupersedingTests.java renamed to junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/LifecycleMethodOverridingAndSupersedingTests.java

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,26 @@
2626
import org.junit.jupiter.api.DisplayName;
2727
import org.junit.jupiter.api.Nested;
2828
import org.junit.jupiter.api.Test;
29+
import org.junit.jupiter.engine.subpackage.SuperClassWithPackagePrivateLifecycleMethodInDifferentPackageTestCase;
2930

3031
/**
31-
* Integration tests that explicitly demonstrate the shadowing/overriding
32+
* Integration tests that explicitly demonstrate the overriding and superseding
3233
* rules for lifecycle methods in the {@link JupiterTestEngine}.
3334
*
3435
* @since 5.9
3536
*/
36-
class LifecycleMethodSupersedingTests {
37+
class LifecycleMethodOverridingAndSupersedingTests {
3738

3839
@Nested
39-
@DisplayName("A package-private lifecycle super-method can be superseded by")
40+
@DisplayName("A package-private lifecycle super-method can be overridden by")
4041
class PackagePrivateSuperClassTests {
4142

4243
@Nested
4344
@DisplayName("a protected lifecycle method in the derived class")
4445
class ProtectedExtendsPackagePrivateLifecycleMethod
4546
extends SuperClassWithPackagePrivateLifecycleMethodTestCase {
4647

48+
@Override
4749
@BeforeEach
4850
protected void beforeEach() {
4951
}
@@ -55,6 +57,7 @@ protected void beforeEach() {
5557
class PackagePrivateExtendsPackagePrivateLifecycleMethod
5658
extends SuperClassWithPackagePrivateLifecycleMethodTestCase {
5759

60+
@Override
5861
@BeforeEach
5962
void beforeEach() {
6063
}
@@ -65,6 +68,7 @@ void beforeEach() {
6568
@DisplayName("a public lifecycle method in the derived class")
6669
class PublicExtendsPackagePrivateLifecycleMethod extends SuperClassWithPackagePrivateLifecycleMethodTestCase {
6770

71+
@Override
6872
@BeforeEach
6973
public void beforeEach() {
7074
}
@@ -73,13 +77,55 @@ public void beforeEach() {
7377
}
7478

7579
@Nested
76-
@DisplayName("A protected lifecycle super-method can be superseded by")
80+
@DisplayName("A package-private lifecycle super-method from a different package can be superseded by")
81+
class PackagePrivateSuperClassInDifferentPackageTests {
82+
83+
@Nested
84+
@DisplayName("a protected lifecycle method in the derived class")
85+
class ProtectedExtendsPackagePrivateLifecycleMethod
86+
extends SuperClassWithPackagePrivateLifecycleMethodInDifferentPackageTestCase {
87+
88+
// @Override
89+
@BeforeEach
90+
protected void beforeEach() {
91+
}
92+
93+
}
94+
95+
@Nested
96+
@DisplayName("a package-private lifecycle method in the derived class")
97+
class PackagePrivateExtendsPackagePrivateLifecycleMethod
98+
extends SuperClassWithPackagePrivateLifecycleMethodInDifferentPackageTestCase {
99+
100+
// @Override
101+
@BeforeEach
102+
void beforeEach() {
103+
}
104+
105+
}
106+
107+
@Nested
108+
@DisplayName("a public lifecycle method in the derived class")
109+
class PublicExtendsPackagePrivateLifecycleMethod
110+
extends SuperClassWithPackagePrivateLifecycleMethodInDifferentPackageTestCase {
111+
112+
// @Override
113+
@BeforeEach
114+
public void beforeEach() {
115+
}
116+
117+
}
118+
}
119+
120+
@Nested
121+
@DisplayName("A protected lifecycle super-method can be overridden by")
77122
class ProtectedSuperClassTests {
78123

79124
@Nested
80125
@DisplayName("a protected lifecycle method in the derived class")
81126
class ProtectedExtendsPackagePrivate extends SuperClassWithProtectedLifecycleMethodTestCase {
82127

128+
@Override
83129
@BeforeEach
84130
protected void beforeEach() {
85131
}
@@ -90,6 +136,7 @@ protected void beforeEach() {
90136
@DisplayName("a public lifecycle method in the derived class")
91137
class PublicExtendsPackagePrivate extends SuperClassWithProtectedLifecycleMethodTestCase {
92138

139+
@Override
93140
@BeforeEach
94141
public void beforeEach() {
95142
}
@@ -98,13 +145,14 @@ public void beforeEach() {
98145
}
99146

100147
@Nested
101-
@DisplayName("A public lifecycle super-method can be superseded by")
148+
@DisplayName("A public lifecycle super-method can be overridden by")
102149
class PublicSuperClassTests {
103150

104151
@Nested
105152
@DisplayName("a public lifecycle method in the derived class")
106153
class PublicExtendsPackagePrivate extends SuperClassWithPublicLifecycleMethodTestCase {
107154

155+
@Override
108156
@BeforeEach
109157
public void beforeEach() {
110158
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2015-2022 the original author or authors.
3+
*
4+
* All rights reserved. This program and the accompanying materials are
5+
* made available under the terms of the Eclipse Public License v2.0 which
6+
* accompanies this distribution and is available at
7+
*
8+
* https://www.eclipse.org/legal/epl-v20.html
9+
*/
10+
11+
package org.junit.jupiter.engine.subpackage;
12+
13+
import static org.junit.jupiter.api.Assertions.fail;
14+
15+
import org.junit.jupiter.api.BeforeEach;
16+
import org.junit.jupiter.api.Test;
17+
18+
/**
19+
* @since 5.9
20+
*/
21+
public class SuperClassWithPackagePrivateLifecycleMethodInDifferentPackageTestCase {
22+
23+
@BeforeEach
24+
void beforeEach() {
25+
fail();
26+
}
27+
28+
@Test
29+
void test() {
30+
}
31+
32+
}

0 commit comments

Comments
 (0)