Simplify by using assertThrows
MODIFY
src/test/java/org/apache/commons/io/IOUtilsTestCase.java
src/test/java/org/apache/commons/io/IOUtilsTestCase.java
@@ -1247,32 +1247,12 @@ private void testToString_URL(final String encoding) throws Exception {
() -> IOUtils.resourceToString("non-existing-file.bin", StandardCharsets.UTF_8, ClassLoader.getSystemClassLoader()));
}
- @Test public void testResourceToString_NullResource() throws Exception {
- boolean exceptionOccurred = false;
-
- try {
- IOUtils.resourceToString(null, StandardCharsets.UTF_8);
- fail();
- } catch (final NullPointerException npe) {
- exceptionOccurred = true;
- assertNotNull(npe);
- }
-
- assertTrue(exceptionOccurred);
- }
-
- @Test public void testResourceToString_NullResource_WithClassLoader() throws Exception {
- boolean exceptionOccurred = false;
-
- try {
- IOUtils.resourceToString(null, StandardCharsets.UTF_8, ClassLoader.getSystemClassLoader());
- fail();
- } catch (final NullPointerException npe) {
- exceptionOccurred = true;
- assertNotNull(npe);
+ @Test public void testResourceToString_NullResource() {
+ assertThrows(NullPointerException.class, () -> IOUtils.resourceToString(null, StandardCharsets.UTF_8));
}
- assertTrue(exceptionOccurred);
+ @Test public void testResourceToString_NullResource_WithClassLoader() {
+ assertThrows(NullPointerException.class, () -> IOUtils.resourceToString(null, StandardCharsets.UTF_8, ClassLoader.getSystemClassLoader()));
}
@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 {
() -> IOUtils.resourceToByteArray("non-existing-file.bin", ClassLoader.getSystemClassLoader()));
}
- @Test public void testResourceToByteArray_Null() throws Exception {
- boolean exceptionOccurred = false;
-
- try {
- IOUtils.resourceToByteArray(null);
- fail();
- } catch (final NullPointerException npe) {
- exceptionOccurred = true;
- assertNotNull(npe);
+ @Test public void testResourceToByteArray_Null() {
+ assertThrows(NullPointerException.class, () -> IOUtils.resourceToByteArray(null));
}
- assertTrue(exceptionOccurred);
- }
-
- @Test public void testResourceToByteArray_Null_WithClassLoader() throws Exception {
- boolean exceptionOccurred = false;
-
- try {
- IOUtils.resourceToByteArray(null, ClassLoader.getSystemClassLoader());
- fail();
- } catch (final NullPointerException npe) {
- exceptionOccurred = true;
- assertNotNull(npe);
- }
-
- assertTrue(exceptionOccurred);
+ @Test public void testResourceToByteArray_Null_WithClassLoader() {
+ assertThrows(NullPointerException.class, () -> IOUtils.resourceToByteArray(null, ClassLoader.getSystemClassLoader()));
}
@Test public void testResourceToURL_ExistingResourceAtRootPackage() throws Exception {
@@ -1387,32 +1347,12 @@ private void testToString_URL(final String encoding) throws Exception {
() -> IOUtils.resourceToURL("non-existing-file.bin", ClassLoader.getSystemClassLoader()));
}
- @Test public void testResourceToURL_Null() throws Exception {
- boolean exceptionOccurred = false;
-
- try {
- IOUtils.resourceToURL(null);
- fail();
- } catch (final NullPointerException npe) {
- exceptionOccurred = true;
- assertNotNull(npe);
+ @Test public void testResourceToURL_Null() {
+ assertThrows(NullPointerException.class, () -> IOUtils.resourceToURL(null));
}
- assertTrue(exceptionOccurred);
- }
-
- @Test public void testResourceToURL_Null_WithClassLoader() throws Exception {
- boolean exceptionOccurred = false;
-
- try {
- IOUtils.resourceToURL(null, ClassLoader.getSystemClassLoader());
- fail();
- } catch (final NullPointerException npe) {
- exceptionOccurred = true;
- assertNotNull(npe);
- }
-
- assertTrue(exceptionOccurred);
+ @Test public void testResourceToURL_Null_WithClassLoader() {
+ assertThrows(NullPointerException.class, () -> IOUtils.resourceToURL(null, ClassLoader.getSystemClassLoader()));
}
@Test public void testAsBufferedNull() {
@@ -1559,12 +1499,7 @@ public void close() throws IOException { }
@Test
public void testAsWriterNull() {
- try {
- IOUtils.writer(null);
- fail("Expected NullPointerException");
- } catch (final NullPointerException npe) {
- // expected
- }
+ assertThrows(NullPointerException.class, () -> IOUtils.writer(null));
}
@Test

Simplify by using assertThrows