Skip to content

Commit 45929df

Browse files
committed
Do not use deprecated JUnit exception testing
1 parent 47887eb commit 45929df

File tree

1 file changed

+100
-105
lines changed

1 file changed

+100
-105
lines changed

src/test/java/com/maxmind/geoip2/WebServiceClientTest.java

Lines changed: 100 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
import junitparams.JUnitParamsRunner;
88
import junitparams.Parameters;
99
import org.hamcrest.CoreMatchers;
10+
import static org.hamcrest.MatcherAssert.assertThat;
1011
import org.junit.Rule;
1112
import org.junit.Test;
12-
import org.junit.rules.ExpectedException;
1313
import org.junit.runner.RunWith;
1414

1515
import java.io.UnsupportedEncodingException;
@@ -26,27 +26,23 @@
2626
@RunWith(JUnitParamsRunner.class)
2727
public class WebServiceClientTest {
2828

29-
@Rule
30-
public final ExpectedException thrown = ExpectedException.none();
31-
3229
@Rule
3330
public final WireMockRule wireMockRule = new WireMockRule(0); // 0 picks random port
3431

3532
@Test
3633
public void test200WithNoBody() throws Exception {
3734
WebServiceClient client = createSuccessClient("insights", "me", "");
3835

39-
thrown.expect(GeoIp2Exception.class);
40-
thrown.expectMessage("Received a 200 response but could not decode it as JSON");
41-
client.insights();
36+
Exception ex = assertThrows(GeoIp2Exception.class, () -> client.insights());
37+
assertEquals("Received a 200 response but could not decode it as JSON", ex.getMessage());
4238
}
4339

4440
@Test
4541
public void test200WithInvalidJson() throws Exception {
4642
WebServiceClient client = createSuccessClient("insights", "me", "{");
47-
thrown.expect(GeoIp2Exception.class);
48-
thrown.expectMessage("Received a 200 response but could not decode it as JSON");
49-
client.insights();
43+
44+
Exception ex = assertThrows(GeoIp2Exception.class, () -> client.insights());
45+
assertEquals("Received a 200 response but could not decode it as JSON", ex.getMessage());
5046
}
5147

5248
@Test
@@ -173,51 +169,50 @@ public void test200OnCountryAsMe() throws Exception {
173169

174170
@Test
175171
public void testAddressNotFound() throws Exception {
176-
thrown.expect(AddressNotFoundException.class);
177-
thrown.expectMessage("not found");
178-
179-
createInsightsError(
180-
"1.2.3.16",
181-
404,
182-
"application/json",
183-
"{\"code\":\"IP_ADDRESS_NOT_FOUND\",\"error\":\"not found\"}"
184-
);
172+
Exception ex = assertThrows(AddressNotFoundException.class,
173+
() -> createInsightsError(
174+
"1.2.3.16",
175+
404,
176+
"application/json",
177+
"{\"code\":\"IP_ADDRESS_NOT_FOUND\",\"error\":\"not found\"}"
178+
));
179+
assertEquals("not found", ex.getMessage());
185180
}
186181

187182
@Test
188183
public void testAddressReserved() throws Exception {
189-
thrown.expect(AddressNotFoundException.class);
190-
thrown.expectMessage("reserved");
191-
createInsightsError(
192-
"1.2.3.17",
193-
400,
194-
"application/json",
195-
"{\"code\":\"IP_ADDRESS_RESERVED\",\"error\":\"reserved\"}"
196-
);
184+
Exception ex = assertThrows(AddressNotFoundException.class,
185+
() -> createInsightsError(
186+
"1.2.3.17",
187+
400,
188+
"application/json",
189+
"{\"code\":\"IP_ADDRESS_RESERVED\",\"error\":\"reserved\"}"
190+
));
191+
assertEquals("reserved", ex.getMessage());
197192
}
198193

199194
@Test
200195
public void testAddressInvalid() throws Exception {
201-
thrown.expect(InvalidRequestException.class);
202-
thrown.expectMessage("invalid");
203-
createInsightsError(
204-
"1.2.3.17",
205-
400,
206-
"application/json",
207-
"{\"code\":\"IP_ADDRESS_INVALID\",\"error\":\"invalid\"}"
208-
);
196+
Exception ex = assertThrows(InvalidRequestException.class,
197+
() -> createInsightsError(
198+
"1.2.3.17",
199+
400,
200+
"application/json",
201+
"{\"code\":\"IP_ADDRESS_INVALID\",\"error\":\"invalid\"}"
202+
));
203+
assertEquals("invalid", ex.getMessage());
209204
}
210205

211206
@Test
212207
@Parameters({"INSUFFICIENT_FUNDS", "OUT_OF_QUERIES"})
213208
public void testInsufficientCredit(String code) throws Exception {
214-
thrown.expect(OutOfQueriesException.class);
215-
thrown.expectMessage("out of credit");
216-
createInsightsMeError(
217-
402,
218-
"application/json",
219-
"{\"code\":\"" + code + "\",\"error\":\"out of credit\"}"
220-
);
209+
Exception ex = assertThrows(OutOfQueriesException.class,
210+
() -> createInsightsMeError(
211+
402,
212+
"application/json",
213+
"{\"code\":\"" + code + "\",\"error\":\"out of credit\"}"
214+
));
215+
assertEquals("out of credit", ex.getMessage());
221216
}
222217

223218
@Test
@@ -228,101 +223,101 @@ public void testInsufficientCredit(String code) throws Exception {
228223
"ACCOUNT_ID_REQUIRED",
229224
"ACCOUNT_ID_UNKNOWN"})
230225
public void testInvalidAuth(String code) throws Exception {
231-
thrown.expect(AuthenticationException.class);
232-
thrown.expectMessage("Invalid auth");
233-
createInsightsMeError(
234-
401,
235-
"application/json",
236-
"{\"code\":\"" + code + "\",\"error\":\"Invalid auth\"}"
237-
);
226+
Exception ex = assertThrows(AuthenticationException.class,
227+
() -> createInsightsMeError(
228+
401,
229+
"application/json",
230+
"{\"code\":\"" + code + "\",\"error\":\"Invalid auth\"}"
231+
));
232+
assertEquals("Invalid auth", ex.getMessage());
238233
}
239234

240235
@Test
241236
public void testPermissionRequired() throws Exception {
242-
thrown.expect(PermissionRequiredException.class);
243-
thrown.expectMessage("Permission required");
244-
createInsightsMeError(
245-
403,
246-
"application/json",
247-
"{\"code\":\"PERMISSION_REQUIRED\",\"error\":\"Permission required\"}"
248-
);
237+
Exception ex = assertThrows(PermissionRequiredException.class,
238+
() -> createInsightsMeError(
239+
403,
240+
"application/json",
241+
"{\"code\":\"PERMISSION_REQUIRED\",\"error\":\"Permission required\"}"
242+
));
243+
assertEquals("Permission required", ex.getMessage());
249244
}
250245

251246
@Test
252247
public void testInvalidRequest() throws Exception {
253-
thrown.expect(InvalidRequestException.class);
254-
thrown.expectMessage("IP invalid");
255-
createInsightsMeError(
256-
400,
257-
"application/json",
258-
"{\"code\":\"IP_ADDRESS_INVALID\",\"error\":\"IP invalid\"}"
259-
);
248+
Exception ex = assertThrows(InvalidRequestException.class,
249+
() -> createInsightsMeError(
250+
400,
251+
"application/json",
252+
"{\"code\":\"IP_ADDRESS_INVALID\",\"error\":\"IP invalid\"}"
253+
));
254+
assertEquals("IP invalid", ex.getMessage());
260255
}
261256

262257
@Test
263258
public void test400WithInvalidJson() throws Exception {
264-
thrown.expect(HttpException.class);
265-
thrown.expectMessage(matchesPattern("Received a 400 error for .*/geoip/v2.1/insights/me but it did not include the expected JSON body: \\{blah\\}"));
266-
createInsightsMeError(
267-
400,
268-
"application/json",
269-
"{blah}"
270-
);
259+
Exception ex = assertThrows(HttpException.class,
260+
() -> createInsightsMeError(
261+
400,
262+
"application/json",
263+
"{blah}"
264+
));
265+
assertThat(ex.getMessage(), matchesPattern("Received a 400 error for .*/geoip/v2.1/insights/me but it did not include the expected JSON body: \\{blah\\}"));
271266
}
272267

273268
@Test
274269
public void test400WithNoBody() throws Exception {
275-
thrown.expect(HttpException.class);
276-
thrown.expectMessage(matchesPattern("Received a 400 error for .*/geoip/v2.1/insights/me but it did not include the expected JSON body: "));
277-
createInsightsMeError(
278-
400,
279-
"application/json",
280-
""
281-
);
270+
Exception ex = assertThrows(HttpException.class,
271+
() -> createInsightsMeError(
272+
400,
273+
"application/json",
274+
""
275+
));
276+
assertThat(ex.getMessage(), matchesPattern("Received a 400 error for .*/geoip/v2.1/insights/me but it did not include the expected JSON body: "));
282277
}
283278

284279
@Test
285280
public void test400WithUnexpectedContentType() throws Exception {
286-
thrown.expect(HttpException.class);
287-
thrown.expectMessage(matchesPattern("Received a 400 error for .*/geoip/v2.1/insights/me but it did not include the expected JSON body: text"));
288-
createInsightsMeError(
289-
400,
290-
"text/plain",
291-
"text"
292-
);
281+
Exception ex = assertThrows(HttpException.class,
282+
() -> createInsightsMeError(
283+
400,
284+
"text/plain",
285+
"text"
286+
));
287+
assertThat(ex.getMessage(), matchesPattern("Received a 400 error for .*/geoip/v2.1/insights/me but it did not include the expected JSON body: text"));
293288
}
294289

295290
@Test
296291
public void test400WithUnexpectedJson() throws Exception {
297-
thrown.expect(HttpException.class);
298-
thrown.expectMessage("Error response contains JSON but it does not specify code or error keys: {\"not\":\"expected\"}");
299-
createInsightsMeError(
300-
400,
301-
"application/json",
302-
"{\"not\":\"expected\"}"
303-
);
292+
Exception ex = assertThrows(HttpException.class,
293+
() -> createInsightsMeError(
294+
400,
295+
"application/json",
296+
"{\"not\":\"expected\"}"
297+
));
298+
assertEquals("Error response contains JSON but it does not specify code or error keys: {\"not\":\"expected\"}", ex.getMessage());
304299
}
305300

306301
@Test
307302
public void test300() throws Exception {
308-
thrown.expect(HttpException.class);
309-
thrown.expectMessage(startsWith("Received an unexpected HTTP status (300)"));
310-
createInsightsMeError(
311-
300,
312-
"application/json",
313-
""
314-
);
303+
Exception ex = assertThrows(HttpException.class,
304+
() -> createInsightsMeError(
305+
300,
306+
"application/json",
307+
""
308+
));
309+
assertThat(ex.getMessage(), startsWith("Received an unexpected HTTP status (300)"));
315310
}
316311

317312
@Test
318313
public void test500() throws Exception {
319-
thrown.expect(HttpException.class);
320-
thrown.expectMessage(startsWith("Received a server error (500)"));
321-
createInsightsMeError(
322-
500,
323-
"application/json",
324-
""
325-
);
314+
Exception ex = assertThrows(HttpException.class,
315+
() -> createInsightsMeError(
316+
500,
317+
"application/json",
318+
""
319+
));
320+
assertThat(ex.getMessage(), startsWith("Received a server error (500)"));
326321
}
327322

328323
private void createInsightsError(String ip, int status, String contentType, String responseContent) throws Exception {

0 commit comments

Comments
 (0)