|
9 | 9 | import lombok.extern.slf4j.Slf4j;
|
10 | 10 | import org.junit.jupiter.api.BeforeEach;
|
11 | 11 | import org.junit.jupiter.api.Test;
|
| 12 | +import org.junit.jupiter.params.ParameterizedTest; |
| 13 | +import org.junit.jupiter.params.provider.MethodSource; |
12 | 14 |
|
13 | 15 | import java.lang.reflect.Type;
|
14 |
| -import java.util.ArrayList; |
15 |
| -import java.util.List; |
| 16 | +import java.util.*; |
16 | 17 |
|
17 | 18 | import static org.assertj.core.api.Assertions.assertThat;
|
18 | 19 | import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
19 |
| -import static org.assertj.core.api.InstanceOfAssertFactories.LIST; |
| 20 | +import static org.assertj.core.api.InstanceOfAssertFactories.COLLECTION; |
20 | 21 |
|
21 | 22 | @Slf4j
|
22 |
| -public class ListGenerationTest { |
| 23 | +public class SingleGenericTypeCollectionGenerationTest { |
23 | 24 | private RandomInstanceGenerator randomInstanceGenerator;
|
| 25 | + private static final Class<?>[] implementedCollectionClasses = {List.class, ArrayList.class, Set.class, HashSet.class, LinkedHashSet.class, TreeSet.class}; |
24 | 26 |
|
25 | 27 | @BeforeEach
|
26 | 28 | public void setUp() {
|
27 | 29 | randomInstanceGenerator = new RandomInstanceGenerator();
|
28 | 30 | }
|
29 |
| - @Test |
30 |
| - public void shouldReturnList() { |
| 31 | + private static Class<?>[] getImplementedCollectionClasses(){ |
| 32 | + return SingleGenericTypeCollectionGenerationTest.implementedCollectionClasses; |
| 33 | + } |
| 34 | + |
| 35 | + @ParameterizedTest |
| 36 | + @MethodSource("getImplementedCollectionClasses") |
| 37 | + public void shouldReturnCollectionOfAppropriateType(Class<?> collectionClass) { |
31 | 38 | //given
|
32 | 39 | Class<?> type = String.class;
|
33 | 40 | //when
|
34 |
| - Object generated = randomInstanceGenerator.generate(List.class, type); |
| 41 | + Object generated = randomInstanceGenerator.generate(collectionClass, type); |
35 | 42 | //then
|
36 |
| - log.info("shouldReturnList : {}", generated); |
| 43 | + log.info("shouldReturn{} : {}",collectionClass.getSimpleName(), generated); |
37 | 44 | assertThat(generated).isNotNull();
|
38 |
| - assertThat(generated).isInstanceOf(List.class); |
| 45 | + assertThat(generated).isInstanceOf(collectionClass); |
39 | 46 |
|
40 | 47 | //asserting type
|
41 |
| - assertThat(generated).asInstanceOf(LIST).first().isInstanceOf(type); |
| 48 | + assertThat(generated).asInstanceOf(COLLECTION).first().isInstanceOf(type); |
42 | 49 | }
|
43 | 50 |
|
44 |
| - @Test |
45 |
| - public void shouldReturnListWithExactSize() { |
| 51 | + @ParameterizedTest |
| 52 | + @MethodSource("getImplementedCollectionClasses") |
| 53 | + public void shouldReturnTheAppropriateCollectionWithExactSize(Class<?> collectionClass) { |
46 | 54 | //given
|
47 | 55 | int size = 20;
|
48 | 56 | Class<?> type = String.class;
|
49 | 57 | //when
|
50 |
| - Object generated = randomInstanceGenerator.generate(List.class, size, type); |
| 58 | + Object generated = randomInstanceGenerator.generate(collectionClass, size, type); |
51 | 59 | //then
|
52 |
| - log.info("shouldReturnListWithExactSize : {}", generated); |
| 60 | + log.info("shouldReturn{}WithExactSize : {}",collectionClass.getSimpleName(), generated); |
53 | 61 | assertThat(generated).isNotNull();
|
54 |
| - assertThat(generated).isInstanceOf(List.class); |
55 |
| - assertThat(generated).asInstanceOf(LIST).hasSize(size); |
| 62 | + assertThat(generated).isInstanceOf(collectionClass); |
| 63 | + assertThat(generated).asInstanceOf(COLLECTION).hasSize(size); |
56 | 64 |
|
57 | 65 | //asserting type
|
58 |
| - assertThat(generated).asInstanceOf(LIST).first().isInstanceOf(type); |
| 66 | + assertThat(generated).asInstanceOf(COLLECTION).first().isInstanceOf(type); |
59 | 67 | }
|
60 | 68 |
|
61 |
| - @Test |
62 |
| - public void shouldReturnListWithSizeBetween() { |
| 69 | + @ParameterizedTest |
| 70 | + @MethodSource("getImplementedCollectionClasses") |
| 71 | + public void shouldReturnTheAppropriateCollectionWithSizeBetween(Class<?> collectionClass) { |
63 | 72 | //given
|
64 | 73 | int minSize = 20;
|
65 | 74 | int maxSize = 40;
|
66 | 75 | Class<?> type = String.class;
|
67 | 76 | //when
|
68 |
| - Object generated = randomInstanceGenerator.generate(List.class, minSize, maxSize, type); |
| 77 | + Object generated = randomInstanceGenerator.generate(collectionClass, minSize, maxSize, type); |
69 | 78 | //then
|
70 | 79 | log.info("shouldReturnListWithSizeBetween : {}", generated);
|
71 | 80 | assertThat(generated).isNotNull();
|
72 |
| - assertThat(generated).isInstanceOf(List.class); |
73 |
| - assertThat(generated).asInstanceOf(LIST).hasSizeBetween(minSize, maxSize); |
| 81 | + assertThat(generated).isInstanceOf(collectionClass); |
| 82 | + assertThat(generated).asInstanceOf(COLLECTION).hasSizeBetween(minSize, maxSize); |
74 | 83 |
|
75 | 84 | //asserting type
|
76 |
| - assertThat(generated).asInstanceOf(LIST).first().isInstanceOf(type); |
| 85 | + assertThat(generated).asInstanceOf(COLLECTION).first().isInstanceOf(type); |
77 | 86 | }
|
78 |
| - @Test |
79 |
| - public void shouldThrowInvalidGenericParamsNumberExceptionWhenTryingToGenerateAListWithAZeroGenericParams() { |
| 87 | + @ParameterizedTest |
| 88 | + @MethodSource("getImplementedCollectionClasses") |
| 89 | + public void shouldThrowInvalidGenericParamsNumberExceptionWhenTryingToGenerateACollectionWithAZeroGenericParams(Class<?> collectionClass) { |
80 | 90 | int required = 1;
|
81 |
| - assertThatThrownBy(() -> randomInstanceGenerator.generate(List.class)) |
| 91 | + assertThatThrownBy(() -> randomInstanceGenerator.generate(collectionClass)) |
82 | 92 | .isInstanceOf(InvalidGenericParametersNumberException.class)
|
83 | 93 | .hasMessage("invalid number of generic parameters, required %d and 0 was found".formatted(required));
|
84 | 94 | }
|
85 | 95 |
|
86 |
| - @Test |
87 |
| - public void shouldThrowInvalidGenericParamsNumberExceptionWhenTryingToGenerateAListWithAMoreThanOneGenericParams() { |
| 96 | + @ParameterizedTest |
| 97 | + @MethodSource("getImplementedCollectionClasses") |
| 98 | + public void shouldThrowInvalidGenericParamsNumberExceptionWhenTryingToGenerateACollectionWithAMoreThanOneGenericParams(Class<?> collectionClass) { |
88 | 99 | int required = 1;
|
89 | 100 | Type[] genericParams = new Type[]{String.class, Integer.class};
|
90 |
| - assertThatThrownBy(() -> randomInstanceGenerator.generate(List.class, genericParams)) |
| 101 | + assertThatThrownBy(() -> randomInstanceGenerator.generate(collectionClass, genericParams)) |
91 | 102 | .isInstanceOf(InvalidGenericParametersNumberException.class)
|
92 |
| - .hasMessage("invalid number of generic parameters, required %d and %d was found".formatted(required, genericParams.length)); |
| 103 | + .hasMessage("invalid number of generic parameters, required %d and %d was found".formatted(required, genericParams.length)); // FIXME is this how should we check for message ? it would be better if we checked a type rather than a string that could be changed in the future without necessarily breaking the code |
93 | 104 | }
|
94 |
| - @Test |
95 |
| - public void shouldThrowIllegalArgumentExceptionGivenMinSizeEqualsMaxSize() { |
| 105 | + @ParameterizedTest |
| 106 | + @MethodSource("getImplementedCollectionClasses") |
| 107 | + public void shouldThrowIllegalArgumentExceptionGivenMinSizeEqualsMaxSize(Class<?> collectionClass) { |
96 | 108 | int size = 30;
|
97 | 109 | assertThatThrownBy(() -> {//when
|
98 |
| - randomInstanceGenerator.generate(ArrayList.class, size, size); |
| 110 | + randomInstanceGenerator.generate(collectionClass, size, size); |
99 | 111 | }).isInstanceOf(IllegalArgumentException.class)
|
100 | 112 | .hasMessage("Start value must be smaller than end value.");
|
101 | 113 | }
|
|
0 commit comments