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
11 import java.nio.CharBuffer;
12
13 import mockit.integration.junit5.JMockitExtension;
14
15 import org.junit.jupiter.api.Test;
16 import org.junit.jupiter.api.extension.ExtendWith;
17
18
19
20
21 @ExtendWith(JMockitExtension.class)
22 class MockedParametersWithCapturingTest {
23
24
25
26
27 public interface Service {
28
29
30
31
32
33
34 int doSomething();
35
36
37
38
39
40
41
42 void doSomethingElse(int i);
43 }
44
45
46
47
48 static final class ServiceImpl implements Service {
49
50
51 final String str;
52
53
54
55
56
57
58
59 ServiceImpl(String str) {
60 this.str = str;
61 }
62
63 @Override
64 public int doSomething() {
65 return 1;
66 }
67
68 @Override
69 public void doSomethingElse(int i) {
70 throw new IllegalMonitorStateException();
71 }
72 }
73
74
75
76
77 static class BaseClass {
78
79
80 final String str;
81
82
83
84
85 BaseClass() {
86 str = "";
87 }
88
89
90
91
92
93
94
95 BaseClass(String str) {
96 this.str = str;
97 }
98
99
100
101
102
103
104 String getStr() {
105 return str;
106 }
107
108
109
110
111 void doSomething() {
112 throw new IllegalStateException("Invalid state");
113 }
114 }
115
116
117
118
119 static class DerivedClass extends BaseClass {
120
121
122
123
124 DerivedClass() {
125 }
126
127
128
129
130
131
132
133 DerivedClass(String str) {
134 super(str);
135 }
136
137 @Override
138 String getStr() {
139 return super.getStr().toUpperCase();
140 }
141 }
142
143
144
145
146
147
148
149 @Test
150 void captureDerivedClass(@Capturing BaseClass service) {
151 assertNull(new DerivedClass("test").str);
152 assertNull(new DerivedClass() {
153 }.str);
154 }
155
156
157
158
159
160
161
162
163
164
165
166
167 @Test
168 void captureImplementationsOfDifferentInterfaces(@Capturing Runnable mock1, @Capturing Readable mock2)
169 throws Exception {
170 Runnable runnable = new Runnable() {
171 @Override
172 public void run() {
173 throw new RuntimeException("run");
174 }
175 };
176 runnable.run();
177
178 Readable readable = new Readable() {
179 @Override
180 public int read(CharBuffer cb) {
181 throw new RuntimeException("read");
182 }
183 };
184 readable.read(CharBuffer.wrap("test"));
185 }
186
187
188
189
190
191
192
193 @Test
194 void captureImplementationsOfAnInterface(@Capturing final Service service) {
195 Service impl1 = new ServiceImpl("test1");
196 impl1.doSomethingElse(1);
197
198 Service impl2 = new Service() {
199 @Override
200 public int doSomething() {
201 return 2;
202 }
203
204 @Override
205 public void doSomethingElse(int i) {
206 throw new IllegalStateException("2");
207 }
208 };
209 impl2.doSomethingElse(2);
210 }
211
212
213
214
215
216
217
218 @Test
219 void captureSubclassesOfABaseClass(@Capturing final BaseClass base) {
220 BaseClass impl1 = new DerivedClass("test1");
221 impl1.doSomething();
222
223 BaseClass impl2 = new BaseClass("test2") {
224 @Override
225 void doSomething() {
226 throw new IllegalStateException("2");
227 }
228 };
229 impl2.doSomething();
230
231 final class DerivedClass2 extends DerivedClass {
232 DerivedClass2() {
233 super("DeRiVed");
234 }
235
236 @Override
237 String getStr() {
238 return super.getStr().toLowerCase();
239 }
240 }
241 DerivedClass2 impl3 = new DerivedClass2();
242 impl3.doSomething();
243 }
244
245
246
247
248 public interface IBase {
249
250
251
252
253
254 int doSomething();
255 }
256
257
258
259
260 public interface ISub extends IBase {
261 }
262
263
264
265
266
267
268
269 @Test
270 void recordCallToBaseInterfaceMethodOnCaptureSubInterfaceImplementation(@Capturing final ISub mock) {
271 new Expectations() {
272 {
273 mock.doSomething();
274 result = 123;
275 }
276 };
277
278 ISub impl = new ISub() {
279 @Override
280 public int doSomething() {
281 return -1;
282 }
283 };
284 int i = impl.doSomething();
285
286 assertEquals(123, i);
287 }
288 }