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