47
47
48
48
import org .junit .jupiter .api .Test ;
49
49
import org .junit .jupiter .api .extension .ParameterContext ;
50
+ import org .junit .jupiter .params .ParameterizedTest ;
51
+ import org .junit .jupiter .params .provider .ValueSource ;
50
52
import org .junit .platform .commons .test .TestClassLoader ;
51
53
import org .junit .platform .commons .util .ReflectionUtils ;
52
54
@@ -61,11 +63,13 @@ class DefaultArgumentConverterTests {
61
63
void isAwareOfNull () {
62
64
assertConverts (null , Object .class , null );
63
65
assertConverts (null , String .class , null );
66
+ assertConverts (null , Boolean .class , null );
64
67
}
65
68
66
69
@ Test
67
70
void isAwareOfWrapperTypesForPrimitiveTypes () {
68
71
assertConverts (true , boolean .class , true );
72
+ assertConverts (false , boolean .class , false );
69
73
assertConverts ((byte ) 1 , byte .class , (byte ) 1 );
70
74
assertConverts ('o' , char .class , 'o' );
71
75
assertConverts ((short ) 1 , short .class , (short ) 1 );
@@ -91,6 +95,7 @@ void isAwareOfWideningConversions() {
91
95
@ Test
92
96
void convertsStringsToPrimitiveTypes () {
93
97
assertConverts ("true" , boolean .class , true );
98
+ assertConverts ("false" , boolean .class , false );
94
99
assertConverts ("o" , char .class , 'o' );
95
100
assertConverts ("1" , byte .class , (byte ) 1 );
96
101
assertConverts ("1_0" , byte .class , (byte ) 10 );
@@ -106,6 +111,54 @@ void convertsStringsToPrimitiveTypes() {
106
111
assertConverts ("42.2_3" , double .class , 42.23 );
107
112
}
108
113
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
+
109
162
@ Test
110
163
void throwsExceptionOnInvalidStringForPrimitiveTypes () {
111
164
assertThatExceptionOfType (ArgumentConversionException .class ) //
0 commit comments