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.assertSame;
10
11 import jakarta.servlet.Servlet;
12 import jakarta.servlet.ServletConfig;
13 import jakarta.servlet.ServletRequest;
14 import jakarta.servlet.ServletResponse;
15
16 import java.net.InetAddress;
17 import java.net.UnknownHostException;
18
19 import mockit.integration.junit5.JMockitExtension;
20
21 import org.junit.jupiter.api.AfterEach;
22 import org.junit.jupiter.api.BeforeEach;
23 import org.junit.jupiter.api.Disabled;
24 import org.junit.jupiter.api.Test;
25 import org.junit.jupiter.api.extension.ExtendWith;
26
27
28
29
30
31 @Disabled
32 @ExtendWith(JMockitExtension.class)
33 final class TestedClassWithConstructorDI2Test {
34
35
36
37
38 public static final class TestedClass implements Servlet {
39
40
41 static int counter;
42
43
44 private ServletConfig config;
45
46
47 private final Dependency dependency1;
48
49
50 private final Dependency dependency2;
51
52
53 private final Dependency dependency3;
54
55
56
57
58
59
60
61
62
63
64
65
66
67 public TestedClass(Dependency dependency1, Runnable r, Dependency dependency2, Dependency dependency3) {
68 this.dependency1 = dependency1;
69 this.dependency2 = dependency2;
70 this.dependency3 = dependency3;
71 r.run();
72
73 int i = dependency1.doSomething();
74 assert i == 123;
75
76 try {
77 InetAddress localHost = InetAddress.getLocalHost();
78 assert localHost.getHostName() == null;
79 } catch (UnknownHostException e) {
80 throw new IllegalStateException("InetAddress should be mocked", e);
81 }
82 }
83
84
85
86
87
88
89 public int doSomeOperation() {
90 return dependency1.doSomething() + dependency2.doSomething();
91 }
92
93 @Override
94 public ServletConfig getServletConfig() {
95 return config;
96 }
97
98 @Override
99 public void service(ServletRequest req, ServletResponse res) {
100 }
101
102 @Override
103 public String getServletInfo() {
104 return null;
105 }
106
107 @Override
108 public void init(ServletConfig cfg) {
109 config = cfg;
110 counter++;
111
112 int i = dependency1.doSomething();
113 assert i == 123;
114
115 checkInetAddressMocking();
116 }
117
118
119
120
121 private void checkInetAddressMocking() {
122 try {
123 InetAddress inetAddress = InetAddress.getByName("testHost");
124 assert inetAddress.getHostName() == null;
125 } catch (UnknownHostException ignore) {
126 counter = -1;
127 }
128 }
129
130 @Override
131 public void destroy() {
132 counter++;
133 checkInetAddressMocking();
134 }
135 }
136
137
138
139
140 static class Dependency {
141
142
143
144
145
146 int doSomething() {
147 return -1;
148 }
149 }
150
151
152 @Tested
153 TestedClass tested;
154
155
156 @Injectable
157 Runnable task;
158
159
160 @Injectable
161 Dependency dependency1;
162
163
164 @Injectable
165 Dependency dependency2;
166
167
168 @Injectable
169 ServletConfig config;
170
171
172 @Mocked
173 InetAddress testHost;
174
175
176
177
178 @BeforeEach
179 void resetCounter() {
180 TestedClass.counter = 0;
181 new Expectations() {
182 {
183 dependency1.doSomething();
184 result = 123;
185 }
186 };
187 }
188
189
190
191
192
193
194
195 @Test
196 void exerciseTestedObjectWithDependenciesOfSameTypeInjectedThroughConstructor(@Injectable Dependency dependency3) {
197 assertTestedObjectWasInitialized();
198 assertSame(dependency3, tested.dependency3);
199
200 new Expectations() {
201 {
202 dependency1.doSomething();
203 result = 23;
204 dependency2.doSomething();
205 result = 5;
206 }
207 };
208
209 assertEquals(28, tested.doSomeOperation());
210 }
211
212
213
214
215
216
217
218
219
220 @Test
221 void exerciseTestedObjectWithExtraInjectableParameter(@Injectable Dependency dependency3,
222 @Injectable Dependency mock4) {
223 assertTestedObjectWasInitialized();
224 assertSame(dependency1, tested.dependency1);
225 assertSame(dependency2, tested.dependency2);
226 assertSame(dependency3, tested.dependency3);
227 }
228
229
230
231
232 void assertTestedObjectWasInitialized() {
233 assertSame(config, tested.getServletConfig());
234 assertEquals(1, TestedClass.counter);
235 }
236
237
238
239
240 @AfterEach
241 void verifyTestedObjectAfterEveryTest() {
242 assertEquals(2, TestedClass.counter);
243 }
244 }