Skip to content

Commit 382e480

Browse files
committed
Simplify by using assertThrows
1 parent 3ddc2f9 commit 382e480

1 file changed

Lines changed: 13 additions & 78 deletions

File tree

src/test/java/org/apache/commons/io/IOUtilsTestCase.java

Lines changed: 13 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,32 +1247,12 @@ private void testToString_URL(final String encoding) throws Exception {
12471247
() -> IOUtils.resourceToString("non-existing-file.bin", StandardCharsets.UTF_8, ClassLoader.getSystemClassLoader()));
12481248
}
12491249

1250-
@Test public void testResourceToString_NullResource() throws Exception {
1251-
boolean exceptionOccurred = false;
1252-
1253-
try {
1254-
IOUtils.resourceToString(null, StandardCharsets.UTF_8);
1255-
fail();
1256-
} catch (final NullPointerException npe) {
1257-
exceptionOccurred = true;
1258-
assertNotNull(npe);
1259-
}
1260-
1261-
assertTrue(exceptionOccurred);
1250+
@Test public void testResourceToString_NullResource() {
1251+
assertThrows(NullPointerException.class, () -> IOUtils.resourceToString(null, StandardCharsets.UTF_8));
12621252
}
12631253

1264-
@Test public void testResourceToString_NullResource_WithClassLoader() throws Exception {
1265-
boolean exceptionOccurred = false;
1266-
1267-
try {
1268-
IOUtils.resourceToString(null, StandardCharsets.UTF_8, ClassLoader.getSystemClassLoader());
1269-
fail();
1270-
} catch (final NullPointerException npe) {
1271-
exceptionOccurred = true;
1272-
assertNotNull(npe);
1273-
}
1274-
1275-
assertTrue(exceptionOccurred);
1254+
@Test public void testResourceToString_NullResource_WithClassLoader() {
1255+
assertThrows(NullPointerException.class, () -> IOUtils.resourceToString(null, StandardCharsets.UTF_8, ClassLoader.getSystemClassLoader()));
12761256
}
12771257

12781258
@SuppressWarnings("squid:S2699") // Suppress "Add at least one assertion to this test case"
@@ -1322,32 +1302,12 @@ private void testToString_URL(final String encoding) throws Exception {
13221302
() -> IOUtils.resourceToByteArray("non-existing-file.bin", ClassLoader.getSystemClassLoader()));
13231303
}
13241304

1325-
@Test public void testResourceToByteArray_Null() throws Exception {
1326-
boolean exceptionOccurred = false;
1327-
1328-
try {
1329-
IOUtils.resourceToByteArray(null);
1330-
fail();
1331-
} catch (final NullPointerException npe) {
1332-
exceptionOccurred = true;
1333-
assertNotNull(npe);
1334-
}
1335-
1336-
assertTrue(exceptionOccurred);
1305+
@Test public void testResourceToByteArray_Null() {
1306+
assertThrows(NullPointerException.class, () -> IOUtils.resourceToByteArray(null));
13371307
}
13381308

1339-
@Test public void testResourceToByteArray_Null_WithClassLoader() throws Exception {
1340-
boolean exceptionOccurred = false;
1341-
1342-
try {
1343-
IOUtils.resourceToByteArray(null, ClassLoader.getSystemClassLoader());
1344-
fail();
1345-
} catch (final NullPointerException npe) {
1346-
exceptionOccurred = true;
1347-
assertNotNull(npe);
1348-
}
1349-
1350-
assertTrue(exceptionOccurred);
1309+
@Test public void testResourceToByteArray_Null_WithClassLoader() {
1310+
assertThrows(NullPointerException.class, () -> IOUtils.resourceToByteArray(null, ClassLoader.getSystemClassLoader()));
13511311
}
13521312

13531313
@Test public void testResourceToURL_ExistingResourceAtRootPackage() throws Exception {
@@ -1387,32 +1347,12 @@ private void testToString_URL(final String encoding) throws Exception {
13871347
() -> IOUtils.resourceToURL("non-existing-file.bin", ClassLoader.getSystemClassLoader()));
13881348
}
13891349

1390-
@Test public void testResourceToURL_Null() throws Exception {
1391-
boolean exceptionOccurred = false;
1392-
1393-
try {
1394-
IOUtils.resourceToURL(null);
1395-
fail();
1396-
} catch (final NullPointerException npe) {
1397-
exceptionOccurred = true;
1398-
assertNotNull(npe);
1399-
}
1400-
1401-
assertTrue(exceptionOccurred);
1350+
@Test public void testResourceToURL_Null() {
1351+
assertThrows(NullPointerException.class, () -> IOUtils.resourceToURL(null));
14021352
}
14031353

1404-
@Test public void testResourceToURL_Null_WithClassLoader() throws Exception {
1405-
boolean exceptionOccurred = false;
1406-
1407-
try {
1408-
IOUtils.resourceToURL(null, ClassLoader.getSystemClassLoader());
1409-
fail();
1410-
} catch (final NullPointerException npe) {
1411-
exceptionOccurred = true;
1412-
assertNotNull(npe);
1413-
}
1414-
1415-
assertTrue(exceptionOccurred);
1354+
@Test public void testResourceToURL_Null_WithClassLoader() {
1355+
assertThrows(NullPointerException.class, () -> IOUtils.resourceToURL(null, ClassLoader.getSystemClassLoader()));
14161356
}
14171357

14181358
@Test public void testAsBufferedNull() {
@@ -1559,12 +1499,7 @@ public void close() throws IOException { }
15591499

15601500
@Test
15611501
public void testAsWriterNull() {
1562-
try {
1563-
IOUtils.writer(null);
1564-
fail("Expected NullPointerException");
1565-
} catch (final NullPointerException npe) {
1566-
// expected
1567-
}
1502+
assertThrows(NullPointerException.class, () -> IOUtils.writer(null));
15681503
}
15691504

15701505
@Test

0 commit comments

Comments
 (0)