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.assertFalse;
10 import static org.junit.jupiter.api.Assertions.assertNotNull;
11 import static org.junit.jupiter.api.Assertions.assertNull;
12 import static org.junit.jupiter.api.Assertions.assertSame;
13 import static org.junit.jupiter.api.Assertions.assertTrue;
14 import static org.junit.jupiter.api.Assertions.fail;
15
16 import java.io.ByteArrayOutputStream;
17 import java.io.File;
18 import java.io.IOException;
19 import java.io.OutputStream;
20 import java.net.HttpURLConnection;
21 import java.net.URL;
22 import java.net.URLConnection;
23 import java.net.URLStreamHandler;
24 import java.nio.file.Path;
25
26 import mockit.integration.junit5.ExpectedException;
27 import mockit.integration.junit5.JMockitExtension;
28
29 import org.junit.jupiter.api.Test;
30 import org.junit.jupiter.api.extension.ExtendWith;
31
32
33
34
35 @ExtendWith(JMockitExtension.class)
36 class ClassLoadingAndJREMocksTest {
37
38
39
40
41 static class Foo {
42 }
43
44
45
46
47 @Test
48 void fakeFile() {
49 new MockUp<File>() {
50 @Mock
51 void $init(String name) {
52 }
53
54 @Mock
55 boolean exists() {
56 return true;
57 }
58 };
59
60 new Foo();
61 assertTrue(Path.of("filePath").toFile().exists());
62 }
63
64
65
66
67 @Test
68 void fakeFileSafelyUsingReentrantFakeMethod() {
69 new MockUp<File>() {
70 @Mock
71 boolean exists(Invocation inv) {
72 File it = inv.getInvokedInstance();
73 return "testFile".equals(it.getName()) || inv.<Boolean> proceed();
74 }
75 };
76
77 checkForTheExistenceOfSeveralFiles();
78 }
79
80
81
82
83 void checkForTheExistenceOfSeveralFiles() {
84 assertFalse(Path.of("someOtherFile").toFile().exists());
85 assertTrue(Path.of("testFile").toFile().exists());
86 assertFalse(Path.of("yet/another/file").toFile().exists());
87 assertTrue(Path.of("testFile").toFile().exists());
88 }
89
90
91
92
93 @Test
94 void fakeFileSafelyUsingProceed() {
95 new MockUp<File>() {
96 @Mock
97 boolean exists(Invocation inv) {
98 File it = inv.getInvokedInstance();
99 return "testFile".equals(it.getName()) || inv.<Boolean> proceed();
100 }
101 };
102
103 checkForTheExistenceOfSeveralFiles();
104 }
105
106
107
108
109
110
111
112 @Test
113 void attemptToMockNonMockableJREClass(@Mocked Integer mock) {
114 assertNull(mock);
115 }
116
117
118
119
120
121
122
123
124
125
126
127
128 @Test
129 void mockURLAndURLConnection(@Mocked URL mockUrl, @Mocked URLConnection mockConnection) throws Exception {
130 URLConnection conn = mockUrl.openConnection();
131
132 assertSame(mockConnection, conn);
133 }
134
135
136
137
138
139
140
141
142
143
144
145
146 @Test
147 void mockURLAndHttpURLConnection(@Mocked URL mockUrl, @Mocked HttpURLConnection mockConnection) throws Exception {
148 HttpURLConnection conn = (HttpURLConnection) mockUrl.openConnection();
149 assertSame(mockConnection, conn);
150 }
151
152
153
154
155
156
157
158
159
160
161 @Test
162 void mockURLAndHttpURLConnectionWithDynamicMock(@Mocked final HttpURLConnection mockHttpConnection)
163 throws Exception {
164 final URL url = new URL("http://nowhere");
165
166 new Expectations(url) {
167 {
168 url.openConnection();
169 result = mockHttpConnection;
170 mockHttpConnection.getOutputStream();
171 result = new ByteArrayOutputStream();
172 }
173 };
174
175
176 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
177 conn.setDoOutput(true);
178 conn.setRequestMethod("PUT");
179 OutputStream out = conn.getOutputStream();
180
181 assertNotNull(out);
182
183 new Verifications() {
184 {
185 mockHttpConnection.setDoOutput(true);
186 mockHttpConnection.setRequestMethod("PUT");
187 }
188 };
189 }
190
191
192
193
194
195
196
197
198
199 String readResourceContent() throws IOException {
200 URL url = new URL("http://remoteHost/aResource");
201 URLConnection connection = url.openConnection();
202
203 connection.setConnectTimeout(1000);
204 connection.connect();
205
206 return connection.getContent().toString();
207 }
208
209
210
211
212
213
214
215
216
217
218
219
220 @Test
221 void cascadingMockedURLWithInjectableCascadedURLConnection(@Mocked URL anyUrl,
222 @Injectable final URLConnection cascadedUrlConnection) throws Exception {
223 String testContent = recordURLConnectionToReturnContent(cascadedUrlConnection);
224
225 String content = readResourceContent();
226
227 assertEquals(testContent, content);
228 }
229
230
231
232
233
234
235
236
237
238
239
240
241 String recordURLConnectionToReturnContent(final URLConnection urlConnection) throws IOException {
242 final String testContent = "testing";
243 new Expectations() {
244 {
245 urlConnection.getContent();
246 result = testContent;
247 }
248 };
249 return testContent;
250 }
251
252
253
254
255
256
257
258
259
260
261 @Test
262 void fakeURLUsingInjectableURLConnection(@Injectable final URLConnection urlConnection) throws Exception {
263 final String testContent = recordURLConnectionToReturnContent(urlConnection);
264 new MockUp<URL>() {
265 @Mock
266 void $init(URL context, String spec, URLStreamHandler handler) {
267 }
268
269 @Mock
270 URLConnection openConnection() {
271 return urlConnection;
272 }
273 };
274
275 String content = readResourceContent();
276
277 assertEquals(testContent, content);
278 }
279
280
281
282
283
284
285
286 @Test
287 @ExpectedException(IllegalArgumentException.class)
288 public void attemptToMockJREClassThatIsNeverMockable(@Mocked Class<?> mockClass) {
289 fail("Should never get here");
290 }
291
292 }