1 package mockit;
2
3 import static org.junit.jupiter.api.Assertions.assertEquals;
4 import static org.junit.jupiter.api.Assertions.assertNull;
5 import static org.junit.jupiter.api.Assertions.assertSame;
6
7 import java.io.Serializable;
8
9 import javax.annotation.Resource;
10 import javax.inject.Inject;
11 import javax.inject.Named;
12 import javax.sql.DataSource;
13
14 import org.junit.jupiter.api.BeforeEach;
15 import org.junit.jupiter.api.Test;
16 import org.springframework.beans.factory.annotation.Autowired;
17 import org.springframework.beans.factory.annotation.Qualifier;
18
19
20
21
22 final class TestedClassWithQualifiedDependencyTest {
23
24
25
26
27 public static class TestedClass {
28
29
30 @Inject
31 private Dependency1 dep1;
32
33
34 @Autowired
35 private Dependency2 dep2;
36
37
38 @Resource(name = "main-db")
39 private DataSource ds;
40
41
42 @Named("some.textual.value")
43 private String text;
44
45
46 @Qualifier("a.BC-12")
47 private Long numericValue;
48
49
50 @Qualifier("a.BC-12b")
51 private Long numericValue2;
52 }
53
54
55
56
57 public static class Dependency1 {
58
59
60 @Inject
61 @Named("action1")
62 private Runnable action;
63
64
65 @Autowired
66 @Qualifier("foo")
67 private Serializable qualifiedDep;
68 }
69
70
71
72
73 public static class Dependency2 {
74
75
76 @Qualifier("action2")
77 private Runnable action;
78
79
80 @Named("bar")
81 private Serializable qualifiedDep;
82 }
83
84
85 @Tested
86 Serializable foo;
87
88
89
90
91 @BeforeEach
92 void createQualifiedDependency() {
93 foo = "foo";
94 }
95
96
97 @Tested
98 Dependency2 dependency2;
99
100
101 @Tested
102 final Long aBC12 = 123L;
103
104
105 @Tested
106 final Long aBC12b = 45L;
107
108
109 @Tested(fullyInitialized = true)
110 TestedClass tested;
111
112
113 @Injectable
114 Runnable action1;
115
116
117
118
119
120
121
122 @Test
123 void useTestedObjectWithDifferentDependenciesEachHavingAQualifiedSubDependency(@Injectable Runnable action2) {
124 assertSame(action2, dependency2.action);
125 assertSame(dependency2, tested.dep2);
126 assertSame(action1, tested.dep1.action);
127 assertSame(foo, tested.dep1.qualifiedDep);
128 assertNull(tested.dep2.qualifiedDep);
129 }
130
131
132 @Tested
133 DependencyImpl dep1;
134
135
136 @Tested(fullyInitialized = true)
137 ClassWithQualifiedDependencyOfAbstractType tested2;
138
139
140
141
142 public interface Dependency {
143 }
144
145
146
147
148 static class DependencyImpl implements Dependency {
149 }
150
151
152
153
154 static class ClassWithQualifiedDependencyOfAbstractType {
155
156 @Named("dep1")
157 Dependency dependency;
158 }
159
160
161
162
163 @Test
164 void useTestedObjectOfSubtypeForQualifiedAbstractDependencyTypeInAnotherTestedObject() {
165 assertSame(dep1, tested2.dependency);
166 }
167
168
169 @Injectable
170 DataSource mainDb;
171
172
173
174
175
176
177
178 @Test
179 void verifyDependenciesHavingCompositeNames(@Injectable("text value") String someTextualValue) {
180 assertSame(mainDb, tested.ds);
181 assertEquals(someTextualValue, tested.text);
182 assertEquals(aBC12, tested.numericValue);
183 assertEquals(aBC12b, tested.numericValue2);
184 }
185 }