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