1
2
3
4
5
6 package mockit;
7
8 import static org.junit.jupiter.api.Assertions.assertNull;
9 import static org.junit.jupiter.api.Assertions.assertSame;
10 import static org.junit.jupiter.api.Assertions.assertTrue;
11
12 import java.io.Serializable;
13 import java.util.Set;
14
15 import mockit.integration.junit5.JMockitExtension;
16
17 import org.junit.jupiter.api.Test;
18 import org.junit.jupiter.api.extension.ExtendWith;
19
20
21
22
23 @ExtendWith(JMockitExtension.class)
24 class TestedMethodsTest {
25
26
27
28
29 public interface Dependency {
30 }
31
32
33
34
35 public interface AnotherDependency {
36 }
37
38
39
40
41 static class DependencyImpl implements Dependency {
42 }
43
44
45
46
47
48
49
50 @SuppressWarnings("unused")
51 public interface BaseDAO<T extends Serializable> {
52 }
53
54
55
56
57 public interface ConcreteDAO extends BaseDAO<String> {
58 }
59
60
61
62
63 static class DAOImpl implements ConcreteDAO {
64 }
65
66
67
68
69 static class TestedClass {
70
71 Dependency dependency;
72
73 ConcreteDAO dao;
74
75 AnotherDependency anotherDependency;
76
77 Set<?> set;
78 }
79
80
81
82
83
84
85
86
87
88 @Tested
89 static Class<? extends Dependency> resolveDependencyInterfaces(Class<Dependency> dependencyInterface) {
90 assertSame(Dependency.class, dependencyInterface);
91 return DependencyImpl.class;
92 }
93
94
95
96
97
98
99
100
101
102 @Tested
103 Class<?> resolveDAOInterfaces(Class<? extends BaseDAO<?>> daoInterface) {
104 assertSame(ConcreteDAO.class, daoInterface);
105 return DAOImpl.class;
106 }
107
108
109
110
111
112
113
114
115
116 @Tested
117 Class<?> resolveAnythingElse(Class<?> anyInterface) {
118 assertSame(AnotherDependency.class, anyInterface);
119 return null;
120 }
121
122
123 @Tested(fullyInitialized = true)
124 TestedClass tested;
125
126
127
128
129 @Test
130 void injectInterfaceImplementationsFromClassesReturnedFromTestedMethods() {
131 assertTrue(tested.dependency instanceof DependencyImpl);
132 assertTrue(tested.dao instanceof DAOImpl);
133 assertNull(tested.anotherDependency);
134 assertNull(tested.set);
135 }
136
137
138
139
140 static final class DAO1 {
141 }
142
143
144
145
146 static final class DAO2 {
147 }
148
149
150
151
152 public interface BaseService {
153 }
154
155
156
157
158
159
160
161 static class BaseServiceImpl<D> {
162
163 D dao;
164 }
165
166
167
168
169 public interface Service1 extends BaseService {
170 }
171
172
173
174
175 public interface Service2 extends BaseService {
176 }
177
178
179
180
181 static final class ConcreteService1 extends BaseServiceImpl<DAO1> implements Service1 {
182 }
183
184
185
186
187 static final class ConcreteService2 extends BaseServiceImpl<DAO2> implements Service2 {
188
189 Service1 service1;
190 }
191
192
193
194
195
196
197
198
199
200 @Tested
201 Class<? extends BaseServiceImpl<?>> resolveServiceInterfaces(Class<? extends BaseService> baseServiceType) {
202 if (baseServiceType == Service1.class) {
203 return ConcreteService1.class;
204 }
205 if (baseServiceType == Service2.class) {
206 return ConcreteService2.class;
207 }
208 return null;
209 }
210
211
212
213
214
215
216
217 @Test
218 void createComplexObjectsWithGenericDependencies(@Tested(fullyInitialized = true) ConcreteService2 service2) {
219 assertTrue(service2.dao instanceof DAO2);
220 Service1 service1 = service2.service1;
221 assertTrue(service1 instanceof ConcreteService1);
222 assertTrue(((ConcreteService1) service1).dao instanceof DAO1);
223 }
224 }