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