1
2
3
4
5
6 package mockit;
7
8 import static java.util.Arrays.asList;
9
10 import static org.junit.jupiter.api.Assertions.assertNull;
11 import static org.junit.jupiter.api.Assertions.assertSame;
12 import static org.junit.jupiter.api.Assertions.assertTrue;
13
14 import jakarta.inject.Inject;
15
16 import java.util.Collection;
17 import java.util.List;
18 import java.util.Set;
19
20 import mockit.integration.junit5.JMockitExtension;
21
22 import org.junit.jupiter.api.Test;
23 import org.junit.jupiter.api.extension.ExtendWith;
24
25
26
27
28 @ExtendWith(JMockitExtension.class)
29 class IterableDITest {
30
31
32
33
34 static class Collaborator {
35
36
37 final int value;
38
39
40
41
42 Collaborator() {
43 value = 0;
44 }
45
46
47
48
49
50
51
52 Collaborator(int value) {
53 this.value = value;
54 }
55 }
56
57
58
59
60 static final class TestedClassWithIterableInjectionPoints {
61
62
63 final List<String> names;
64
65
66 @Inject
67 Collection<Collaborator> collaborators;
68
69
70 Set<? extends Number> numbers;
71
72
73
74
75
76
77
78 @Inject
79 TestedClassWithIterableInjectionPoints(List<String> names) {
80 this.names = names;
81 }
82 }
83
84
85 @Injectable
86 final List<String> nameList = asList("One", "Two");
87
88
89 @Injectable
90 final Collection<Collaborator> colList = asList(new Collaborator(1), new Collaborator(2));
91
92
93 @Tested
94 TestedClassWithIterableInjectionPoints tested1;
95
96
97
98
99 @Test
100 void injectMultiValuedInjectablesIntoInjectionPointsOfTheSameCollectionTypes() {
101 assertSame(nameList, tested1.names);
102 assertSame(colList, tested1.collaborators);
103 assertNull(tested1.numbers);
104 }
105
106
107
108
109 static class Dependency {
110 }
111
112
113
114
115 static class SubDependency extends Dependency {
116 }
117
118
119
120
121 static class TestedClassWithInjectedList {
122
123
124 @Inject
125 List<Dependency> dependencies;
126
127
128 Set<String> names;
129 }
130
131
132 @Tested
133 TestedClassWithInjectedList tested2;
134
135
136 @Injectable
137 Dependency dependency;
138
139
140
141
142 @Test
143 void injectMockedInstanceIntoList() {
144 assertTrue(tested2.dependencies.contains(dependency));
145 }
146
147
148
149
150
151
152
153 @Test
154 void doNotInjectStringIntoUnannotatedSet(@Injectable("test") String name) {
155 assertNull(tested2.names);
156 }
157
158
159
160
161
162
163
164 @Test
165 void injectSubTypeInstanceIntoListOfBaseType(@Injectable SubDependency sub) {
166 assertTrue(tested2.dependencies.contains(sub));
167 }
168 }