|
11 | 11 | package org.junit.jupiter.params.converter;
|
12 | 12 |
|
13 | 13 | import static org.assertj.core.api.Assertions.assertThat;
|
| 14 | +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
14 | 15 |
|
15 | 16 | import java.io.File;
|
16 | 17 | import java.lang.Thread.State;
|
@@ -99,6 +100,40 @@ void convertsStringsToPrimitiveTypes() {
|
99 | 100 | assertConverts("42.2_3", double.class, 42.23);
|
100 | 101 | }
|
101 | 102 |
|
| 103 | + @Test |
| 104 | + void throwsExceptionOnInvalidStringForPrimitiveTypes() { |
| 105 | + assertThatExceptionOfType(ArgumentConversionException.class) // |
| 106 | + .isThrownBy(() -> convert("ab", char.class)) // |
| 107 | + .withMessage("Failed to convert String \"ab\" to type java.lang.Character") // |
| 108 | + .havingCause() // |
| 109 | + .withMessage("String must have length of 1: ab"); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + void throwsExceptionWhenImplicitConverstionIsUnsupported() { |
| 114 | + assertThatExceptionOfType(ArgumentConversionException.class) // |
| 115 | + .isThrownBy(() -> convert("foo", Enigma.class)) // |
| 116 | + .withMessage("No implicit conversion to convert object of type java.lang.String to type %s", |
| 117 | + Enigma.class.getName()); |
| 118 | + |
| 119 | + assertThatExceptionOfType(ArgumentConversionException.class) // |
| 120 | + .isThrownBy(() -> convert(new Enigma(), int[].class)) // |
| 121 | + .withMessage("No implicit conversion to convert object of type %s to type [I", Enigma.class.getName()); |
| 122 | + |
| 123 | + assertThatExceptionOfType(ArgumentConversionException.class) // |
| 124 | + .isThrownBy(() -> convert(new long[] {}, int[].class)) // |
| 125 | + .withMessage("No implicit conversion to convert object of type [J to type [I"); |
| 126 | + |
| 127 | + assertThatExceptionOfType(ArgumentConversionException.class) // |
| 128 | + .isThrownBy(() -> convert(new String[] {}, boolean.class)) // |
| 129 | + .withMessage( |
| 130 | + "No implicit conversion to convert object of type [Ljava.lang.String; to type java.lang.Boolean"); |
| 131 | + |
| 132 | + assertThatExceptionOfType(ArgumentConversionException.class) // |
| 133 | + .isThrownBy(() -> convert(Class.class, int[].class)) // |
| 134 | + .withMessage("No implicit conversion to convert object of type java.lang.Class to type [I"); |
| 135 | + } |
| 136 | + |
102 | 137 | /**
|
103 | 138 | * @since 5.4
|
104 | 139 | */
|
@@ -232,11 +267,18 @@ void convertsStringToUUID() {
|
232 | 267 | // -------------------------------------------------------------------------
|
233 | 268 |
|
234 | 269 | private void assertConverts(Object input, Class<?> targetClass, Object expectedOutput) {
|
235 |
| - var result = DefaultArgumentConverter.INSTANCE.convert(input, targetClass); |
| 270 | + var result = convert(input, targetClass); |
236 | 271 |
|
237 | 272 | assertThat(result) //
|
238 | 273 | .describedAs(input + " --(" + targetClass.getName() + ")--> " + expectedOutput) //
|
239 | 274 | .isEqualTo(expectedOutput);
|
240 | 275 | }
|
241 | 276 |
|
| 277 | + private Object convert(Object input, Class<?> targetClass) { |
| 278 | + return DefaultArgumentConverter.INSTANCE.convert(input, targetClass); |
| 279 | + } |
| 280 | + |
| 281 | + private static class Enigma { |
| 282 | + } |
| 283 | + |
242 | 284 | }
|
0 commit comments