Skip to content

Commit 3480fc8

Browse files
committed
Allow @RegisterExtension fields to be private
Prior to this commit, an exception was thrown if a @RegisterExtension field was declared private. This commit removes this restriction. Closes #2688 See #864, #2680
1 parent 4ae957d commit 3480fc8

File tree

5 files changed

+17
-26
lines changed

5 files changed

+17
-26
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ registered last and _after_ callback extensions to be registered first, relative
125125
programmatically registered extensions.
126126
====
127127

128-
NOTE: `@RegisterExtension` fields must not be `private` or `null` (at evaluation time) but
129-
may be either `static` or non-static.
128+
NOTE: `@RegisterExtension` fields must not be `null` (at evaluation time) but may be
129+
either `static` or non-static.
130130

131131
[[extensions-registration-programmatic-static-fields]]
132132
===== Static Fields
@@ -159,9 +159,9 @@ include::{testDir}/example/registration/WebServerDemo.java[tags=user_guide]
159159

160160
The Kotlin programming language does not have the concept of a `static` field. However,
161161
the compiler can be instructed to generate static fields using annotations. Since, as
162-
stated earlier, `@RegisterExtension` fields must not be `private` nor `null`, one
163-
**cannot** use the `@JvmStatic` annotation in Kotlin as it generates `private` fields.
164-
Rather, the `@JvmField` annotation must be used.
162+
stated earlier, `@RegisterExtension` fields must not be `null`, one **cannot** use the
163+
`@JvmStatic` annotation in Kotlin as it generates `private` fields. Rather, the
164+
`@JvmField` annotation must be used.
165165

166166
The following example is a version of the `WebServerDemo` from the previous section that
167167
has been ported to Kotlin.

junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/RegisterExtension.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
* to pass arguments to the extension's constructor, {@code static} factory
3131
* method, or builder API.
3232
*
33-
* <p>{@code @RegisterExtension} fields must not be {@code private} or
34-
* {@code null} (when evaluated) but may be either {@code static} or non-static.
33+
* <p>{@code @RegisterExtension} fields must not be {@code null} (when evaluated)
34+
* but may be either {@code static} or non-static.
3535
*
3636
* <h3>Static Fields</h3>
3737
*

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import static org.junit.platform.commons.util.ReflectionUtils.HierarchyTraversalMode.TOP_DOWN;
1717
import static org.junit.platform.commons.util.ReflectionUtils.findFields;
1818
import static org.junit.platform.commons.util.ReflectionUtils.getDeclaredConstructor;
19-
import static org.junit.platform.commons.util.ReflectionUtils.isNotPrivate;
2019
import static org.junit.platform.commons.util.ReflectionUtils.tryToReadFieldValue;
2120

2221
import java.lang.reflect.AnnotatedElement;
@@ -108,9 +107,6 @@ static void registerExtensionsFromFields(ExtensionRegistrar registrar, Class<?>
108107
fields.stream()
109108
.filter(field -> isAnnotated(field, RegisterExtension.class))
110109
.forEach(field -> {
111-
Preconditions.condition(isNotPrivate(field), () -> String.format(
112-
"Failed to register extension via @RegisterExtension field [%s]: field must not be private.",
113-
field));
114110
tryToReadFieldValue(field, instance).ifSuccess(value -> {
115111
Preconditions.condition(value instanceof Extension, () -> String.format(
116112
"Failed to register extension via @RegisterExtension field [%s]: field value's type [%s] must implement an [%s] API.",

junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/extension/ExtensionRegistrationViaParametersAndFieldsTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ private static TestTemplateInvocationContext emptyTestTemplateInvocationContext(
533533
static class StaticFieldTestCase {
534534

535535
@MagicField
536-
static String staticField1;
536+
private static String staticField1;
537537

538538
@MagicField
539539
static String staticField2;
@@ -560,7 +560,7 @@ static class InstanceFieldTestCase {
560560
String instanceField1;
561561

562562
@MagicField
563-
String instanceField2;
563+
private String instanceField2;
564564

565565
@Test
566566
void test() {
@@ -606,15 +606,15 @@ static class AllInOneWithTestInstancePerMethodTestCase {
606606

607607
@RegisterExtension
608608
@Order(1)
609-
static Extension classLevelExtension1 = new ClassLevelExtension1();
609+
private static Extension classLevelExtension1 = new ClassLevelExtension1();
610610

611611
@RegisterExtension
612612
@Order(2)
613613
static Extension classLevelExtension2 = new ClassLevelExtension2();
614614

615615
@RegisterExtension
616616
@Order(1)
617-
Extension instanceLevelExtension1 = new InstanceLevelExtension1();
617+
private Extension instanceLevelExtension1 = new InstanceLevelExtension1();
618618

619619
@RegisterExtension
620620
@Order(2)

junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/extension/ProgrammaticExtensionRegistrationTests.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,7 @@ void classLevelWithFieldThatDoesNotImplementAnExtensionApi() {
167167
@Test
168168
void instanceLevelWithPrivateField() {
169169
Class<?> testClass = InstanceLevelExtensionRegistrationWithPrivateFieldTestCase.class;
170-
executeTestsForClass(testClass).testEvents().assertThatEvents().haveExactly(1, finishedWithFailure(
171-
instanceOf(PreconditionViolationException.class), message(expectedNotPrivateMessage(testClass))));
170+
executeTestsForClass(testClass).testEvents().assertStatistics(stats -> stats.succeeded(1));
172171
}
173172

174173
/**
@@ -177,8 +176,7 @@ void instanceLevelWithPrivateField() {
177176
@Test
178177
void classLevelWithPrivateField() {
179178
Class<?> testClass = ClassLevelExtensionRegistrationWithPrivateFieldTestCase.class;
180-
executeTestsForClass(testClass).containerEvents().assertThatEvents().haveExactly(1, finishedWithFailure(
181-
instanceOf(PreconditionViolationException.class), message(expectedNotPrivateMessage(testClass))));
179+
executeTestsForClass(testClass).testEvents().assertStatistics(stats -> stats.succeeded(1));
182180
}
183181

184182
@Test
@@ -219,11 +217,6 @@ void classLevelWithNonExtensionFieldValue() {
219217
instanceOf(PreconditionViolationException.class), message(expectedMessage(testClass, String.class))));
220218
}
221219

222-
private String expectedNotPrivateMessage(Class<?> testClass) {
223-
return "Failed to register extension via @RegisterExtension field [" + field(testClass)
224-
+ "]: field must not be private.";
225-
}
226-
227220
private String expectedMessage(Class<?> testClass, Class<?> valueType) {
228221
return "Failed to register extension via @RegisterExtension field [" + field(testClass)
229222
+ "]: field value's type [" + (valueType != null ? valueType.getName() : null) + "] must implement an ["
@@ -602,14 +595,16 @@ void test() {
602595
static class InstanceLevelExtensionRegistrationWithPrivateFieldTestCase extends AbstractTestCase {
603596

604597
@RegisterExtension
605-
private Extension extension;
598+
private Extension extension = new Extension() {
599+
};
606600

607601
}
608602

609603
static class ClassLevelExtensionRegistrationWithPrivateFieldTestCase extends AbstractTestCase {
610604

611605
@RegisterExtension
612-
private static Extension extension;
606+
private static Extension extension = new Extension() {
607+
};
613608

614609
}
615610

0 commit comments

Comments
 (0)