Skip to content

Commit 738b13c

Browse files
committed
Test status quo for null/"null" conversion support in parameterized tests
See #3472
1 parent 2ab6828 commit 738b13c

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
@@ -102,6 +102,40 @@
102102
*/
103103
class ParameterizedTestIntegrationTests {
104104

105+
@ParameterizedTest
106+
@CsvSource(textBlock = """
107+
apple, True
108+
banana, true
109+
lemon, false
110+
kumquat, null
111+
""")
112+
void sweetFruit(String fruit, Boolean sweet) {
113+
switch (fruit) {
114+
case "apple" -> assertThat(sweet).isTrue();
115+
case "banana" -> assertThat(sweet).isTrue();
116+
case "lemon" -> assertThat(sweet).isFalse();
117+
case "kumquat" -> assertThat(sweet).isFalse(); // "null" --> false
118+
default -> fail("Unexpected fruit : " + fruit);
119+
}
120+
}
121+
122+
@ParameterizedTest
123+
@CsvSource(nullValues = "null", textBlock = """
124+
apple, True
125+
banana, true
126+
lemon, false
127+
kumquat, null
128+
""")
129+
void sweetFruitWithNullableBoolean(String fruit, Boolean sweet) {
130+
switch (fruit) {
131+
case "apple" -> assertThat(sweet).isTrue();
132+
case "banana" -> assertThat(sweet).isTrue();
133+
case "lemon" -> assertThat(sweet).isFalse();
134+
case "kumquat" -> assertThat(sweet).isNull(); // null --> null
135+
default -> fail("Unexpected fruit : " + fruit);
136+
}
137+
}
138+
105139
@ParameterizedTest
106140
@CsvSource(quoteCharacter = '"', textBlock = """
107141

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
@@ -43,6 +43,8 @@
4343
import java.util.concurrent.TimeUnit;
4444

4545
import org.junit.jupiter.api.Test;
46+
import org.junit.jupiter.params.ParameterizedTest;
47+
import org.junit.jupiter.params.provider.ValueSource;
4648

4749
/**
4850
* Unit tests for {@link DefaultArgumentConverter}.
@@ -55,11 +57,13 @@ class DefaultArgumentConverterTests {
5557
void isAwareOfNull() {
5658
assertConverts(null, Object.class, null);
5759
assertConverts(null, String.class, null);
60+
assertConverts(null, Boolean.class, null);
5861
}
5962

6063
@Test
6164
void isAwareOfWrapperTypesForPrimitiveTypes() {
6265
assertConverts(true, boolean.class, true);
66+
assertConverts(false, boolean.class, false);
6367
assertConverts((byte) 1, byte.class, (byte) 1);
6468
assertConverts('o', char.class, 'o');
6569
assertConverts((short) 1, short.class, (short) 1);
@@ -85,6 +89,7 @@ void isAwareOfWideningConversions() {
8589
@Test
8690
void convertsStringsToPrimitiveTypes() {
8791
assertConverts("true", boolean.class, true);
92+
assertConverts("false", boolean.class, false);
8893
assertConverts("o", char.class, 'o');
8994
assertConverts("1", byte.class, (byte) 1);
9095
assertConverts("1_0", byte.class, (byte) 10);
@@ -100,6 +105,54 @@ void convertsStringsToPrimitiveTypes() {
100105
assertConverts("42.2_3", double.class, 42.23);
101106
}
102107

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

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

252253
var arguments = provideArguments(annotation);
253254

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

257258
@Test

0 commit comments

Comments
 (0)