|
17 | 17 | package org.springframework.core.convert.support;
|
18 | 18 |
|
19 | 19 | import org.junit.jupiter.api.Test;
|
| 20 | +import org.junit.jupiter.params.ParameterizedTest; |
| 21 | +import org.junit.jupiter.params.provider.CsvSource; |
| 22 | + |
| 23 | +import org.springframework.core.convert.converter.Converter; |
20 | 24 |
|
21 | 25 | import static org.assertj.core.api.Assertions.assertThat;
|
22 | 26 | import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType;
|
23 | 27 |
|
24 | 28 | /**
|
| 29 | + * Unit tests for {@link IntegerToEnumConverterFactory}. |
| 30 | + * |
25 | 31 | * @author Adilson Antunes
|
| 32 | + * @author Sam Brannen |
26 | 33 | */
|
27 | 34 | class IntegerToEnumConverterFactoryTests {
|
28 | 35 |
|
| 36 | + private final Converter<Integer, Color> converter = new IntegerToEnumConverterFactory().getConverter(Color.class); |
29 | 37 |
|
30 |
| - enum Colors { |
31 |
| - RED, |
32 |
| - BLUE, |
33 |
| - GREEN |
34 |
| - } |
35 | 38 |
|
36 |
| - @Test |
37 |
| - void convertIntegerToEnum() { |
38 |
| - final IntegerToEnumConverterFactory enumConverterFactory = new IntegerToEnumConverterFactory(); |
39 |
| - assertThat(enumConverterFactory.getConverter(Colors.class).convert(0)).isEqualTo(Colors.RED); |
40 |
| - assertThat(enumConverterFactory.getConverter(Colors.class).convert(1)).isEqualTo(Colors.BLUE); |
41 |
| - assertThat(enumConverterFactory.getConverter(Colors.class).convert(2)).isEqualTo(Colors.GREEN); |
| 39 | + @ParameterizedTest |
| 40 | + @CsvSource({ |
| 41 | + "0, RED", |
| 42 | + "1, BLUE", |
| 43 | + "2, GREEN" |
| 44 | + }) |
| 45 | + void convertsIntegerToEnum(int index, Color color) { |
| 46 | + assertThat(converter.convert(index)).isEqualTo(color); |
42 | 47 | }
|
43 | 48 |
|
44 | 49 | @Test
|
45 | 50 | void throwsArrayIndexOutOfBoundsExceptionIfInvalidEnumInteger() {
|
46 |
| - final IntegerToEnumConverterFactory enumConverterFactory = new IntegerToEnumConverterFactory(); |
47 | 51 | assertThatExceptionOfType(ArrayIndexOutOfBoundsException.class)
|
48 |
| - .isThrownBy(() -> enumConverterFactory.getConverter(Colors.class).convert(999)); |
| 52 | + .isThrownBy(() -> converter.convert(999)); |
49 | 53 | }
|
50 | 54 |
|
51 | 55 |
|
| 56 | + enum Color { |
| 57 | + RED, |
| 58 | + BLUE, |
| 59 | + GREEN |
| 60 | + } |
| 61 | + |
52 | 62 | }
|
0 commit comments