Skip to content

Commit 0f27653

Browse files
bjmimarcphilipp
andauthored
Support void and Void class lookups in ReflectionUtils (#4049)
Both the primitive type `void` and the wrapper type `Void` are now supported in the internal `ReflectionUtils` to allow `String` to `Class` conversion in parameterized tests. Fixes #4048. --------- Co-authored-by: Marc Philipp <[email protected]>
1 parent dceac6e commit 0f27653

File tree

4 files changed

+12
-2
lines changed

4 files changed

+12
-2
lines changed

documentation/src/docs/asciidoc/release-notes/release-notes-5.12.0-M1.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ JUnit repository on GitHub.
2929

3030
* Introduce `ReflectionSupport.makeAccessible(Field)` for third-party use rather than
3131
calling the internal `ReflectionUtils.makeAccessible(Field)` method directly.
32+
* Support both the primitive type `void` and the wrapper type `Void` in the internal
33+
`ReflectionUtils` to support `String` to `Class` conversion in parameterized tests.
3234

3335

3436
[[release-notes-5.12.0-M1-junit-jupiter]]

junit-platform-commons/src/main/java/org/junit/platform/commons/util/ReflectionUtils.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ public enum HierarchyTraversalMode {
186186
long.class,
187187
float.class,
188188
double.class,
189+
void.class,
189190

190191
boolean[].class,
191192
byte[].class,
@@ -213,6 +214,7 @@ public enum HierarchyTraversalMode {
213214
Long.class,
214215
Float.class,
215216
Double.class,
217+
Void.class,
216218
String.class,
217219

218220
Boolean[].class,
@@ -246,7 +248,7 @@ public enum HierarchyTraversalMode {
246248

247249
classNameToTypeMap = Collections.unmodifiableMap(classNamesToTypes);
248250

249-
Map<Class<?>, Class<?>> primitivesToWrappers = new IdentityHashMap<>(8);
251+
Map<Class<?>, Class<?>> primitivesToWrappers = new IdentityHashMap<>(9);
250252

251253
primitivesToWrappers.put(boolean.class, Boolean.class);
252254
primitivesToWrappers.put(byte.class, Byte.class);
@@ -256,6 +258,7 @@ public enum HierarchyTraversalMode {
256258
primitivesToWrappers.put(long.class, Long.class);
257259
primitivesToWrappers.put(float.class, Float.class);
258260
primitivesToWrappers.put(double.class, Double.class);
261+
primitivesToWrappers.put(void.class, Void.class);
259262

260263
primitiveToWrapperMap = Collections.unmodifiableMap(primitivesToWrappers);
261264
}

jupiter-tests/src/test/java/org/junit/jupiter/params/converter/DefaultArgumentConverterTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ void convertsStringsToPrimitiveWrapperTypes() {
132132

133133
@ParameterizedTest(name = "[{index}] {0}")
134134
@ValueSource(classes = { char.class, boolean.class, short.class, byte.class, int.class, long.class, float.class,
135-
double.class })
135+
double.class, void.class })
136136
void throwsExceptionForNullToPrimitiveTypeConversion(Class<?> type) {
137137
assertThatExceptionOfType(ArgumentConversionException.class) //
138138
.isThrownBy(() -> convert(null, type)) //
@@ -261,8 +261,10 @@ void convertsStringToPath() {
261261
@Test
262262
void convertsStringToClass() {
263263
assertConverts("java.lang.Integer", Class.class, Integer.class);
264+
assertConverts("java.lang.Void", Class.class, Void.class);
264265
assertConverts("java.lang.Thread$State", Class.class, State.class);
265266
assertConverts("byte", Class.class, byte.class);
267+
assertConverts("void", Class.class, void.class);
266268
assertConverts("char[]", Class.class, char[].class);
267269
assertConverts("java.lang.Long[][]", Class.class, Long[][].class);
268270
assertConverts("[[[I", Class.class, int[][][].class);

platform-tests/src/test/java/org/junit/platform/commons/util/ReflectionUtilsTests.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,7 @@ void isAssignableTo() {
559559
// Wrappers to Primitives
560560
assertTrue(ReflectionUtils.isAssignableTo(Integer.class, int.class));
561561
assertTrue(ReflectionUtils.isAssignableTo(Boolean.class, boolean.class));
562+
assertTrue(ReflectionUtils.isAssignableTo(Void.class, void.class));
562563

563564
// Widening Conversions from Wrappers to Primitives
564565
assertTrue(ReflectionUtils.isAssignableTo(Integer.class, long.class));
@@ -759,6 +760,7 @@ void loadClass() {
759760
@Test
760761
void tryToLoadClass() {
761762
assertThat(ReflectionUtils.tryToLoadClass(Integer.class.getName())).isEqualTo(success(Integer.class));
763+
assertThat(ReflectionUtils.tryToLoadClass(Void.class.getName())).isEqualTo(success(Void.class));
762764
}
763765

764766
@Test
@@ -770,6 +772,7 @@ void tryToLoadClassTrimsClassName() {
770772
@Test
771773
void tryToLoadClassForPrimitive() {
772774
assertThat(ReflectionUtils.tryToLoadClass(int.class.getName())).isEqualTo(success(int.class));
775+
assertThat(ReflectionUtils.tryToLoadClass(void.class.getName())).isEqualTo(success(void.class));
773776
}
774777

775778
@Test

0 commit comments

Comments
 (0)