From d65b9d2bed09847ef28b9ceb30c9f8a96e0c12fe Mon Sep 17 00:00:00 2001 From: Allon Mureinik Date: Sat, 30 Sep 2017 15:32:54 +0300 Subject: [PATCH] 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) {} --- src/test/java/org/apache/commons/lang3/CharUtilsTest.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/test/java/org/apache/commons/lang3/CharUtilsTest.java b/src/test/java/org/apache/commons/lang3/CharUtilsTest.java index 71975d3a0..50f263460 100644 --- a/src/test/java/org/apache/commons/lang3/CharUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/CharUtilsTest.java @@ -21,6 +21,7 @@ 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 void testToChar_Character() { 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 void testToChar_String() { 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 void testToIntValue_char() { 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 void testToIntValue_Character() { 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) {} }