Skip to content

Commit 849ce5e

Browse files
committed
Test 5.9.x behavior for null/"null" conversion support in parameterized tests
2 tests in this commit currently fail due to changes in 5.10 with the failure message: Failed to convert String "null" to type java.lang.Boolean This will be addressed in a subsequent commit to align with the expected behavior in 5.10.x. See #3472
1 parent 7cfae80 commit 849ce5e

File tree

3 files changed

+90
-2
lines changed

3 files changed

+90
-2
lines changed

junit-jupiter-params/src/test/java/org/junit/jupiter/params/ParameterizedTestIntegrationTests.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,40 @@
111111
*/
112112
class ParameterizedTestIntegrationTests {
113113

114+
@ParameterizedTest
115+
@CsvSource(textBlock = """
116+
apple, True
117+
banana, true
118+
lemon, false
119+
kumquat, null
120+
""")
121+
void sweetFruit(String fruit, Boolean sweet) {
122+
switch (fruit) {
123+
case "apple" -> assertThat(sweet).isTrue();
124+
case "banana" -> assertThat(sweet).isTrue();
125+
case "lemon" -> assertThat(sweet).isFalse();
126+
case "kumquat" -> assertThat(sweet).isFalse(); // "null" --> false
127+
default -> fail("Unexpected fruit : " + fruit);
128+
}
129+
}
130+
131+
@ParameterizedTest
132+
@CsvSource(nullValues = "null", textBlock = """
133+
apple, True
134+
banana, true
135+
lemon, false
136+
kumquat, null
137+
""")
138+
void sweetFruitWithNullableBoolean(String fruit, Boolean sweet) {
139+
switch (fruit) {
140+
case "apple" -> assertThat(sweet).isTrue();
141+
case "banana" -> assertThat(sweet).isTrue();
142+
case "lemon" -> assertThat(sweet).isFalse();
143+
case "kumquat" -> assertThat(sweet).isNull(); // null --> null
144+
default -> fail("Unexpected fruit : " + fruit);
145+
}
146+
}
147+
114148
@ParameterizedTest
115149
@CsvSource(quoteCharacter = '"', textBlock = """
116150

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747

4848
import org.junit.jupiter.api.Test;
4949
import org.junit.jupiter.api.extension.ParameterContext;
50+
import org.junit.jupiter.params.ParameterizedTest;
51+
import org.junit.jupiter.params.provider.ValueSource;
5052
import org.junit.platform.commons.test.TestClassLoader;
5153
import org.junit.platform.commons.util.ReflectionUtils;
5254

@@ -61,11 +63,13 @@ class DefaultArgumentConverterTests {
6163
void isAwareOfNull() {
6264
assertConverts(null, Object.class, null);
6365
assertConverts(null, String.class, null);
66+
assertConverts(null, Boolean.class, null);
6467
}
6568

6669
@Test
6770
void isAwareOfWrapperTypesForPrimitiveTypes() {
6871
assertConverts(true, boolean.class, true);
72+
assertConverts(false, boolean.class, false);
6973
assertConverts((byte) 1, byte.class, (byte) 1);
7074
assertConverts('o', char.class, 'o');
7175
assertConverts((short) 1, short.class, (short) 1);
@@ -91,6 +95,7 @@ void isAwareOfWideningConversions() {
9195
@Test
9296
void convertsStringsToPrimitiveTypes() {
9397
assertConverts("true", boolean.class, true);
98+
assertConverts("false", boolean.class, false);
9499
assertConverts("o", char.class, 'o');
95100
assertConverts("1", byte.class, (byte) 1);
96101
assertConverts("1_0", byte.class, (byte) 10);
@@ -106,6 +111,54 @@ void convertsStringsToPrimitiveTypes() {
106111
assertConverts("42.2_3", double.class, 42.23);
107112
}
108113

114+
@Test
115+
void convertsStringsToPrimitiveWrapperTypes() {
116+
assertConverts("true", Boolean.class, true);
117+
assertConverts("false", Boolean.class, false);
118+
assertConverts("o", Character.class, 'o');
119+
assertConverts("1", Byte.class, (byte) 1);
120+
assertConverts("1_0", Byte.class, (byte) 10);
121+
assertConverts("1", Short.class, (short) 1);
122+
assertConverts("1_2", Short.class, (short) 12);
123+
assertConverts("42", Integer.class, 42);
124+
assertConverts("700_050_000", Integer.class, 700_050_000);
125+
assertConverts("42", Long.class, 42L);
126+
assertConverts("4_2", Long.class, 42L);
127+
assertConverts("42.23", Float.class, 42.23f);
128+
assertConverts("42.2_3", Float.class, 42.23f);
129+
assertConverts("42.23", Double.class, 42.23);
130+
assertConverts("42.2_3", Double.class, 42.23);
131+
}
132+
133+
@Test
134+
void convertsTheWordNullToBooleanFalse() {
135+
assertConverts("null", boolean.class, false);
136+
assertConverts("NULL", boolean.class, false);
137+
assertConverts("null", Boolean.class, false);
138+
assertConverts("NULL", Boolean.class, false);
139+
}
140+
141+
@ParameterizedTest(name = "[{index}] {0}")
142+
@ValueSource(classes = { char.class, boolean.class, short.class, byte.class, int.class, long.class, float.class,
143+
double.class })
144+
void throwsExceptionForNullToPrimitiveTypeConversion(Class<?> type) {
145+
assertThatExceptionOfType(ArgumentConversionException.class) //
146+
.isThrownBy(() -> convert(null, type)) //
147+
.withMessage("Cannot convert null to primitive value of type " + type.getCanonicalName());
148+
}
149+
150+
@ParameterizedTest(name = "[{index}] {0}")
151+
// NOTE: everything except Boolean.class and Character.class.
152+
@ValueSource(classes = { Short.class, Byte.class, Integer.class, Long.class, Float.class, Double.class })
153+
void throwsExceptionWhenConvertingTheWordNullToPrimitiveWrapperType(Class<?> type) {
154+
assertThatExceptionOfType(ArgumentConversionException.class) //
155+
.isThrownBy(() -> convert("null", type)) //
156+
.withMessage("Failed to convert String \"null\" to type " + type.getCanonicalName());
157+
assertThatExceptionOfType(ArgumentConversionException.class) //
158+
.isThrownBy(() -> convert("NULL", type)) //
159+
.withMessage("Failed to convert String \"NULL\" to type " + type.getCanonicalName());
160+
}
161+
109162
@Test
110163
void throwsExceptionOnInvalidStringForPrimitiveTypes() {
111164
assertThatExceptionOfType(ArgumentConversionException.class) //

junit-jupiter-params/src/test/java/org/junit/jupiter/params/provider/CsvArgumentsProviderTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,11 +248,12 @@ void customEmptyValueAndDefaultNullValue() {
248248

249249
@Test
250250
void customNullValues() {
251-
var annotation = csvSource().nullValues("N/A", "NIL").lines("apple, , NIL, '', N/A, banana").build();
251+
var annotation = csvSource().nullValues("N/A", "NIL", "null")//
252+
.lines("apple, , NIL, '', N/A, banana, null").build();
252253

253254
var arguments = provideArguments(annotation);
254255

255-
assertThat(arguments).containsExactly(array("apple", null, null, "", null, "banana"));
256+
assertThat(arguments).containsExactly(array("apple", null, null, "", null, "banana", null));
256257
}
257258

258259
@Test

0 commit comments

Comments
 (0)