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