Improve tests for CharUtils illegal usages (closes #293)

CharUtilsTest has several instances of the following pattern:

    try {
        CharUtils.someMethod("illegal input");
    } catch (final IllegalArgumentException ex) {}

This pattern is not very useful for testing, as the test would pass
whether an IllegalArgumentException is thrown or not. This patch
enhances the test by explicitly failing it if the exception is not
thrown:

    try {
        CharUtils.someMethod("illegal input");
        fail("An IllegalArgumentException should have been thrown");
    } catch (final IllegalArgumentException ex) {}
This commit is contained in:
Allon Mureinik 2017-09-30 15:32:54 +03:00 committed by pascalschumacher
parent ae862ae116
commit d65b9d2bed
1 changed files with 7 additions and 0 deletions

View File

@ -21,6 +21,7 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
@ -84,6 +85,7 @@ public class CharUtilsTest {
assertEquals('B', CharUtils.toChar(CHARACTER_B));
try {
CharUtils.toChar((Character) null);
fail("An IllegalArgumentException should have been thrown");
} catch (final IllegalArgumentException ex) {}
}
@ -100,9 +102,11 @@ public class CharUtilsTest {
assertEquals('B', CharUtils.toChar("BA"));
try {
CharUtils.toChar((String) null);
fail("An IllegalArgumentException should have been thrown");
} catch (final IllegalArgumentException ex) {}
try {
CharUtils.toChar("");
fail("An IllegalArgumentException should have been thrown");
} catch (final IllegalArgumentException ex) {}
}
@ -128,6 +132,7 @@ public class CharUtilsTest {
assertEquals(9, CharUtils.toIntValue('9'));
try {
CharUtils.toIntValue('a');
fail("An IllegalArgumentException should have been thrown");
} catch (final IllegalArgumentException ex) {}
}
@ -144,9 +149,11 @@ public class CharUtilsTest {
assertEquals(3, CharUtils.toIntValue(new Character('3')));
try {
CharUtils.toIntValue(null);
fail("An IllegalArgumentException should have been thrown");
} catch (final IllegalArgumentException ex) {}
try {
CharUtils.toIntValue(CHARACTER_A);
fail("An IllegalArgumentException should have been thrown");
} catch (final IllegalArgumentException ex) {}
}