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