You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by pa...@apache.org on 2018/10/13 19:11:08 UTC
[04/14] [lang] Clean up testing of exceptions
http://git-wip-us.apache.org/repos/asf/commons-lang/blob/3387734b/src/test/java/org/apache/commons/lang3/reflect/FieldUtilsTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/lang3/reflect/FieldUtilsTest.java b/src/test/java/org/apache/commons/lang3/reflect/FieldUtilsTest.java
index b9fbff5..d542b72 100644
--- a/src/test/java/org/apache/commons/lang3/reflect/FieldUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/reflect/FieldUtilsTest.java
@@ -43,7 +43,6 @@ import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
/**
@@ -373,47 +372,35 @@ public class FieldUtilsTest {
assertEquals(Foo.VALUE, FieldUtils.readStaticField(PrivatelyShadowedChild.class, "VALUE"));
assertEquals(Foo.VALUE, FieldUtils.readStaticField(PublicChild.class, "VALUE"));
- try {
- FieldUtils.readStaticField(null, "none");
- fail("null class should cause an IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // expected
- }
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> FieldUtils.readStaticField(null, "none"),
+ "null class should cause an IllegalArgumentException");
- try {
- FieldUtils.readStaticField(Foo.class, null);
- fail("null field name should cause an IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // expected
- }
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> FieldUtils.readStaticField(Foo.class, null),
+ "null field name should cause an IllegalArgumentException");
- try {
- FieldUtils.readStaticField(Foo.class, "");
- fail("empty field name should cause an IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // expected
- }
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> FieldUtils.readStaticField(Foo.class, ""),
+ "empty field name should cause an IllegalArgumentException");
- try {
- FieldUtils.readStaticField(Foo.class, " ");
- fail("blank field name should cause an IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // expected
- }
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> FieldUtils.readStaticField(Foo.class, " "),
+ "blank field name should cause an IllegalArgumentException");
- try {
- FieldUtils.readStaticField(Foo.class, "does_not_exist");
- fail("a field that doesn't exist should cause an IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // expected
- }
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> FieldUtils.readStaticField(Foo.class, "does_not_exist"),
+ "a field that doesn't exist should cause an IllegalArgumentException");
- try {
- FieldUtils.readStaticField(PublicChild.class, "s");
- fail("non-static field should cause an IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // expected
- }
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> FieldUtils.readStaticField(PublicChild.class, "s"),
+ "non-static field should cause an IllegalArgumentException");
}
@Test
@@ -423,88 +410,60 @@ public class FieldUtilsTest {
assertEquals(Foo.VALUE, FieldUtils.readStaticField(PrivatelyShadowedChild.class, "VALUE", true));
assertEquals("child", FieldUtils.readStaticField(PublicChild.class, "VALUE", true));
- try {
- FieldUtils.readStaticField(null, "none", true);
- fail("null class should cause an IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // expected
- }
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> FieldUtils.readStaticField(null, "none", true),
+ "null class should cause an IllegalArgumentException");
- try {
- FieldUtils.readStaticField(Foo.class, null, true);
- fail("null field name should cause an IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // expected
- }
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> FieldUtils.readStaticField(Foo.class, null, true),
+ "null field name should cause an IllegalArgumentException");
- try {
- FieldUtils.readStaticField(Foo.class, "", true);
- fail("empty field name should cause an IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // expected
- }
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> FieldUtils.readStaticField(Foo.class, "", true),
+ "empty field name should cause an IllegalArgumentException");
- try {
- FieldUtils.readStaticField(Foo.class, " ", true);
- fail("blank field name should cause an IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // expected
- }
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> FieldUtils.readStaticField(Foo.class, " ", true),
+ "blank field name should cause an IllegalArgumentException");
- try {
- FieldUtils.readStaticField(Foo.class, "does_not_exist", true);
- fail("a field that doesn't exist should cause an IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // expected
- }
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> FieldUtils.readStaticField(Foo.class, "does_not_exist", true),
+ "a field that doesn't exist should cause an IllegalArgumentException");
- try {
- FieldUtils.readStaticField(PublicChild.class, "s", false);
- fail("non-static field should cause an IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // expected
- }
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> FieldUtils.readStaticField(PublicChild.class, "s", false),
+ "non-static field should cause an IllegalArgumentException");
}
@Test
public void testReadDeclaredNamedStaticField() throws Exception {
assertEquals(Foo.VALUE, FieldUtils.readDeclaredStaticField(Foo.class, "VALUE"));
- try {
- FieldUtils.readDeclaredStaticField(PublicChild.class, "VALUE");
- fail("expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // pass
- }
- try {
- FieldUtils.readDeclaredStaticField(PubliclyShadowedChild.class, "VALUE");
- fail("expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // pass
- }
- try {
- FieldUtils.readDeclaredStaticField(PrivatelyShadowedChild.class, "VALUE");
- fail("expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // pass
- }
+ assertThrows(
+ IllegalArgumentException.class, () -> FieldUtils.readDeclaredStaticField(PublicChild.class, "VALUE"));
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> FieldUtils.readDeclaredStaticField(PubliclyShadowedChild.class, "VALUE"));
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> FieldUtils.readDeclaredStaticField(PrivatelyShadowedChild.class, "VALUE"));
}
@Test
public void testReadDeclaredNamedStaticFieldForceAccess() throws Exception {
assertEquals(Foo.VALUE, FieldUtils.readDeclaredStaticField(Foo.class, "VALUE", true));
assertEquals("child", FieldUtils.readDeclaredStaticField(PublicChild.class, "VALUE", true));
- try {
- FieldUtils.readDeclaredStaticField(PubliclyShadowedChild.class, "VALUE", true);
- fail("expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // pass
- }
- try {
- FieldUtils.readDeclaredStaticField(PrivatelyShadowedChild.class, "VALUE", true);
- fail("expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // pass
- }
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> FieldUtils.readDeclaredStaticField(PubliclyShadowedChild.class, "VALUE", true));
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> FieldUtils.readDeclaredStaticField(PrivatelyShadowedChild.class, "VALUE", true));
}
@Test
@@ -526,12 +485,10 @@ public class FieldUtilsTest {
assertEquals(D0, FieldUtils.readField(parentD, publiclyShadowedChild));
assertEquals(D0, FieldUtils.readField(parentD, privatelyShadowedChild));
- try {
- FieldUtils.readField(null, publicChild);
- fail("a null field should cause an IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // expected
- }
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> FieldUtils.readField(null, publicChild),
+ "a null field should cause an IllegalArgumentException");
}
@Test
@@ -557,12 +514,10 @@ public class FieldUtilsTest {
assertEquals(D0, FieldUtils.readField(parentD, publiclyShadowedChild, true));
assertEquals(D0, FieldUtils.readField(parentD, privatelyShadowedChild, true));
- try {
- FieldUtils.readField(null, publicChild, true);
- fail("a null field should cause an IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // expected
- }
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> FieldUtils.readField(null, publicChild, true),
+ "a null field should cause an IllegalArgumentException");
}
@Test
@@ -571,73 +526,36 @@ public class FieldUtilsTest {
assertEquals("ss", FieldUtils.readField(publiclyShadowedChild, "s"));
assertEquals("s", FieldUtils.readField(privatelyShadowedChild, "s"));
- try {
- FieldUtils.readField(publicChild, null);
- fail("a null field name should cause an IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // expected
- }
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> FieldUtils.readField(publicChild, null),
+ "a null field name should cause an IllegalArgumentException");
- try {
- FieldUtils.readField(publicChild, "");
- fail("an empty field name should cause an IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // expected
- }
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> FieldUtils.readField(publicChild, ""),
+ "an empty field name should cause an IllegalArgumentException");
- try {
- FieldUtils.readField(publicChild, " ");
- fail("a blank field name should cause an IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // expected
- }
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> FieldUtils.readField(publicChild, " "),
+ "a blank field name should cause an IllegalArgumentException");
- try {
- FieldUtils.readField((Object) null, "none");
- fail("a null target should cause an IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // expected
- }
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> FieldUtils.readField((Object) null, "none"),
+ "a null target should cause an IllegalArgumentException");
+
+ assertThrows(IllegalArgumentException.class, () -> FieldUtils.readField(publicChild, "b"));
- try {
- FieldUtils.readField(publicChild, "b");
- fail("expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // pass
- }
assertEquals(Boolean.TRUE, FieldUtils.readField(publiclyShadowedChild, "b"));
- try {
- FieldUtils.readField(privatelyShadowedChild, "b");
- fail("expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // pass
- }
- try {
- FieldUtils.readField(publicChild, "i");
- fail("expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // pass
- }
+ assertThrows( IllegalArgumentException.class, () -> FieldUtils.readField(privatelyShadowedChild, "b"));
+ assertThrows(IllegalArgumentException.class, () -> FieldUtils.readField(publicChild, "i"));
assertEquals(I1, FieldUtils.readField(publiclyShadowedChild, "i"));
- try {
- FieldUtils.readField(privatelyShadowedChild, "i");
- fail("expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // pass
- }
- try {
- FieldUtils.readField(publicChild, "d");
- fail("expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // pass
- }
+ assertThrows(IllegalArgumentException.class, () -> FieldUtils.readField(privatelyShadowedChild, "i"));
+ assertThrows(IllegalArgumentException.class, () -> FieldUtils.readField(publicChild, "d"));
assertEquals(D1, FieldUtils.readField(publiclyShadowedChild, "d"));
- try {
- FieldUtils.readField(privatelyShadowedChild, "d");
- fail("expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // pass
- }
+ assertThrows(IllegalArgumentException.class, () -> FieldUtils.readField(privatelyShadowedChild, "d"));
}
@Test
@@ -655,179 +573,95 @@ public class FieldUtilsTest {
assertEquals(D1, FieldUtils.readField(publiclyShadowedChild, "d", true));
assertEquals(D1, FieldUtils.readField(privatelyShadowedChild, "d", true));
- try {
- FieldUtils.readField(publicChild, null, true);
- fail("a null field name should cause an IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // expected
- }
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> FieldUtils.readField(publicChild, null, true),
+ "a null field name should cause an IllegalArgumentException");
- try {
- FieldUtils.readField(publicChild, "", true);
- fail("an empty field name should cause an IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // expected
- }
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> FieldUtils.readField(publicChild, "", true),
+ "an empty field name should cause an IllegalArgumentException");
- try {
- FieldUtils.readField(publicChild, " ", true);
- fail("a blank field name should cause an IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // expected
- }
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> FieldUtils.readField(publicChild, " ", true),
+ "a blank field name should cause an IllegalArgumentException");
- try {
- FieldUtils.readField((Object) null, "none", true);
- fail("a null target should cause an IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // expected
- }
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> FieldUtils.readField((Object) null, "none", true),
+ "a null target should cause an IllegalArgumentException");
}
@Test
public void testReadDeclaredNamedField() throws Exception {
- try {
- FieldUtils.readDeclaredField(publicChild, null);
- fail("a null field name should cause an IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // expected
- }
-
- try {
- FieldUtils.readDeclaredField(publicChild, "");
- fail("an empty field name should cause an IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // expected
- }
-
- try {
- FieldUtils.readDeclaredField(publicChild, " ");
- fail("a blank field name should cause an IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // expected
- }
-
- try {
- FieldUtils.readDeclaredField(null, "none");
- fail("a null target should cause an IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // expected
- }
-
- try {
- FieldUtils.readDeclaredField(publicChild, "s");
- fail("expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // pass
- }
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> FieldUtils.readDeclaredField(publicChild, null),
+ "a null field name should cause an IllegalArgumentException");
+
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> FieldUtils.readDeclaredField(publicChild, ""),
+ "an empty field name should cause an IllegalArgumentException");
+
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> FieldUtils.readDeclaredField(publicChild, " "),
+ "a blank field name should cause an IllegalArgumentException");
+
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> FieldUtils.readDeclaredField(null, "none"),
+ "a null target should cause an IllegalArgumentException");
+
+ assertThrows(IllegalArgumentException.class, () -> FieldUtils.readDeclaredField(publicChild, "s"));
assertEquals("ss", FieldUtils.readDeclaredField(publiclyShadowedChild, "s"));
- try {
- FieldUtils.readDeclaredField(privatelyShadowedChild, "s");
- fail("expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // pass
- }
- try {
- FieldUtils.readDeclaredField(publicChild, "b");
- fail("expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // pass
- }
+ assertThrows(IllegalArgumentException.class, () -> FieldUtils.readDeclaredField(privatelyShadowedChild, "s"));
+ assertThrows(IllegalArgumentException.class, () -> FieldUtils.readDeclaredField(publicChild, "b"));
assertEquals(Boolean.TRUE, FieldUtils.readDeclaredField(publiclyShadowedChild, "b"));
- try {
- FieldUtils.readDeclaredField(privatelyShadowedChild, "b");
- fail("expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // pass
- }
- try {
- FieldUtils.readDeclaredField(publicChild, "i");
- fail("expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // pass
- }
+ assertThrows(IllegalArgumentException.class, () -> FieldUtils.readDeclaredField(privatelyShadowedChild, "b"));
+ assertThrows(IllegalArgumentException.class, () -> FieldUtils.readDeclaredField(publicChild, "i"));
assertEquals(I1, FieldUtils.readDeclaredField(publiclyShadowedChild, "i"));
- try {
- FieldUtils.readDeclaredField(privatelyShadowedChild, "i");
- fail("expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // pass
- }
- try {
- FieldUtils.readDeclaredField(publicChild, "d");
- fail("expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // pass
- }
+ assertThrows(IllegalArgumentException.class, () -> FieldUtils.readDeclaredField(privatelyShadowedChild, "i"));
+ assertThrows(IllegalArgumentException.class, () -> FieldUtils.readDeclaredField(publicChild, "d"));
assertEquals(D1, FieldUtils.readDeclaredField(publiclyShadowedChild, "d"));
- try {
- FieldUtils.readDeclaredField(privatelyShadowedChild, "d");
- fail("expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // pass
- }
+ assertThrows(IllegalArgumentException.class, () -> FieldUtils.readDeclaredField(privatelyShadowedChild, "d"));
}
@Test
public void testReadDeclaredNamedFieldForceAccess() throws Exception {
- try {
- FieldUtils.readDeclaredField(publicChild, null, true);
- fail("a null field name should cause an IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // expected
- }
-
- try {
- FieldUtils.readDeclaredField(publicChild, "", true);
- fail("an empty field name should cause an IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // expected
- }
-
- try {
- FieldUtils.readDeclaredField(publicChild, " ", true);
- fail("a blank field name should cause an IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // expected
- }
-
- try {
- FieldUtils.readDeclaredField(null, "none", true);
- fail("a null target should cause an IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // expected
- }
-
- try {
- FieldUtils.readDeclaredField(publicChild, "s", true);
- fail("expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // pass
- }
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> FieldUtils.readDeclaredField(publicChild, null, true),
+ "a null field name should cause an IllegalArgumentException");
+
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> FieldUtils.readDeclaredField(publicChild, "", true),
+ "an empty field name should cause an IllegalArgumentException");
+
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> FieldUtils.readDeclaredField(publicChild, " ", true),
+ "a blank field name should cause an IllegalArgumentException");
+
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> FieldUtils.readDeclaredField(null, "none", true),
+ "a null target should cause an IllegalArgumentException");
+
+ assertThrows(IllegalArgumentException.class, () -> FieldUtils.readDeclaredField(publicChild, "s", true));
assertEquals("ss", FieldUtils.readDeclaredField(publiclyShadowedChild, "s", true));
assertEquals("ss", FieldUtils.readDeclaredField(privatelyShadowedChild, "s", true));
- try {
- FieldUtils.readDeclaredField(publicChild, "b", true);
- fail("expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // pass
- }
+ assertThrows(IllegalArgumentException.class, () -> FieldUtils.readDeclaredField(publicChild, "b", true));
assertEquals(Boolean.TRUE, FieldUtils.readDeclaredField(publiclyShadowedChild, "b", true));
assertEquals(Boolean.TRUE, FieldUtils.readDeclaredField(privatelyShadowedChild, "b", true));
- try {
- FieldUtils.readDeclaredField(publicChild, "i", true);
- fail("expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // pass
- }
+ assertThrows(IllegalArgumentException.class, () -> FieldUtils.readDeclaredField(publicChild, "i", true));
assertEquals(I1, FieldUtils.readDeclaredField(publiclyShadowedChild, "i", true));
assertEquals(I1, FieldUtils.readDeclaredField(privatelyShadowedChild, "i", true));
- try {
- FieldUtils.readDeclaredField(publicChild, "d", true);
- fail("expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // pass
- }
+ assertThrows(IllegalArgumentException.class, () -> FieldUtils.readDeclaredField(publicChild, "d", true));
assertEquals(D1, FieldUtils.readDeclaredField(publiclyShadowedChild, "d", true));
assertEquals(D1, FieldUtils.readDeclaredField(privatelyShadowedChild, "d", true));
}
@@ -837,55 +671,27 @@ public class FieldUtilsTest {
Field field = StaticContainer.class.getDeclaredField("mutablePublic");
FieldUtils.writeStaticField(field, "new");
assertEquals("new", StaticContainer.mutablePublic);
- field = StaticContainer.class.getDeclaredField("mutableProtected");
- try {
- FieldUtils.writeStaticField(field, "new");
- fail("Expected IllegalAccessException");
- } catch (final IllegalAccessException e) {
- // pass
- }
- field = StaticContainer.class.getDeclaredField("mutablePackage");
- try {
- FieldUtils.writeStaticField(field, "new");
- fail("Expected IllegalAccessException");
- } catch (final IllegalAccessException e) {
- // pass
- }
- field = StaticContainer.class.getDeclaredField("mutablePrivate");
- try {
- FieldUtils.writeStaticField(field, "new");
- fail("Expected IllegalAccessException");
- } catch (final IllegalAccessException e) {
- // pass
- }
- field = StaticContainer.class.getDeclaredField("IMMUTABLE_PUBLIC");
- try {
- FieldUtils.writeStaticField(field, "new");
- fail("Expected IllegalAccessException");
- } catch (final IllegalAccessException e) {
- // pass
- }
- field = StaticContainer.class.getDeclaredField("IMMUTABLE_PROTECTED");
- try {
- FieldUtils.writeStaticField(field, "new");
- fail("Expected IllegalAccessException");
- } catch (final IllegalAccessException e) {
- // pass
- }
- field = StaticContainer.class.getDeclaredField("IMMUTABLE_PACKAGE");
- try {
- FieldUtils.writeStaticField(field, "new");
- fail("Expected IllegalAccessException");
- } catch (final IllegalAccessException e) {
- // pass
- }
- field = StaticContainer.class.getDeclaredField("IMMUTABLE_PRIVATE");
- try {
- FieldUtils.writeStaticField(field, "new");
- fail("Expected IllegalAccessException");
- } catch (final IllegalAccessException e) {
- // pass
- }
+ assertThrows(
+ IllegalAccessException.class,
+ () -> FieldUtils.writeStaticField(StaticContainer.class.getDeclaredField("mutableProtected"), "new"));
+ assertThrows(
+ IllegalAccessException.class,
+ () -> FieldUtils.writeStaticField(StaticContainer.class.getDeclaredField("mutablePackage"), "new"));
+ assertThrows(
+ IllegalAccessException.class,
+ () -> FieldUtils.writeStaticField(StaticContainer.class.getDeclaredField("mutablePrivate"), "new"));
+ assertThrows(
+ IllegalAccessException.class,
+ () -> FieldUtils.writeStaticField(StaticContainer.class.getDeclaredField("IMMUTABLE_PUBLIC"), "new"));
+ assertThrows(
+ IllegalAccessException.class,
+ () -> FieldUtils.writeStaticField(StaticContainer.class.getDeclaredField("IMMUTABLE_PROTECTED"), "new"));
+ assertThrows(
+ IllegalAccessException.class,
+ () -> FieldUtils.writeStaticField(StaticContainer.class.getDeclaredField("IMMUTABLE_PACKAGE"), "new"));
+ assertThrows(
+ IllegalAccessException.class,
+ () -> FieldUtils.writeStaticField(StaticContainer.class.getDeclaredField("IMMUTABLE_PRIVATE"), "new"));
}
@Test
@@ -902,82 +708,45 @@ public class FieldUtilsTest {
field = StaticContainer.class.getDeclaredField("mutablePrivate");
FieldUtils.writeStaticField(field, "new", true);
assertEquals("new", StaticContainer.getMutablePrivate());
- field = StaticContainer.class.getDeclaredField("IMMUTABLE_PUBLIC");
- try {
- FieldUtils.writeStaticField(field, "new", true);
- fail("Expected IllegalAccessException");
- } catch (final IllegalAccessException e) {
- // pass
- }
- field = StaticContainer.class.getDeclaredField("IMMUTABLE_PROTECTED");
- try {
- FieldUtils.writeStaticField(field, "new", true);
- fail("Expected IllegalAccessException");
- } catch (final IllegalAccessException e) {
- // pass
- }
- field = StaticContainer.class.getDeclaredField("IMMUTABLE_PACKAGE");
- try {
- FieldUtils.writeStaticField(field, "new", true);
- fail("Expected IllegalAccessException");
- } catch (final IllegalAccessException e) {
- // pass
- }
- field = StaticContainer.class.getDeclaredField("IMMUTABLE_PRIVATE");
- try {
- FieldUtils.writeStaticField(field, "new", true);
- fail("Expected IllegalAccessException");
- } catch (final IllegalAccessException e) {
- // pass
- }
+ assertThrows(
+ IllegalAccessException.class,
+ () -> FieldUtils.writeStaticField(StaticContainer.class.getDeclaredField("IMMUTABLE_PUBLIC"), "new", true));
+ assertThrows(
+ IllegalAccessException.class,
+ () -> FieldUtils.writeStaticField(StaticContainer.class.getDeclaredField("IMMUTABLE_PROTECTED"), "new", true));
+ assertThrows(
+ IllegalAccessException.class,
+ () -> FieldUtils.writeStaticField(StaticContainer.class.getDeclaredField("IMMUTABLE_PACKAGE"), "new", true));
+ assertThrows(
+ IllegalAccessException.class,
+ () -> FieldUtils.writeStaticField(StaticContainer.class.getDeclaredField("IMMUTABLE_PRIVATE"), "new", true));
}
@Test
public void testWriteNamedStaticField() throws Exception {
FieldUtils.writeStaticField(StaticContainerChild.class, "mutablePublic", "new");
assertEquals("new", StaticContainer.mutablePublic);
- try {
- FieldUtils.writeStaticField(StaticContainerChild.class, "mutableProtected", "new");
- fail("Expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // pass
- }
- try {
- FieldUtils.writeStaticField(StaticContainerChild.class, "mutablePackage", "new");
- fail("Expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // pass
- }
- try {
- FieldUtils.writeStaticField(StaticContainerChild.class, "mutablePrivate", "new");
- fail("Expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // pass
- }
- try {
- FieldUtils.writeStaticField(StaticContainerChild.class, "IMMUTABLE_PUBLIC", "new");
- fail("Expected IllegalAccessException");
- } catch (final IllegalAccessException e) {
- // pass
- }
- try {
- FieldUtils.writeStaticField(StaticContainerChild.class, "IMMUTABLE_PROTECTED", "new");
- fail("Expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // pass
- }
- try {
- FieldUtils.writeStaticField(StaticContainerChild.class, "IMMUTABLE_PACKAGE", "new");
- fail("Expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // pass
- }
- try {
- FieldUtils.writeStaticField(StaticContainerChild.class, "IMMUTABLE_PRIVATE", "new");
- fail("Expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // pass
- }
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> FieldUtils.writeStaticField(StaticContainerChild.class, "mutableProtected", "new"));
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> FieldUtils.writeStaticField(StaticContainerChild.class, "mutablePackage", "new"));
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> FieldUtils.writeStaticField(StaticContainerChild.class, "mutablePrivate", "new"));
+ assertThrows(
+ IllegalAccessException.class,
+ () -> FieldUtils.writeStaticField(StaticContainerChild.class, "IMMUTABLE_PUBLIC", "new"));
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> FieldUtils.writeStaticField(StaticContainerChild.class, "IMMUTABLE_PROTECTED", "new"));
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> FieldUtils.writeStaticField(StaticContainerChild.class, "IMMUTABLE_PACKAGE", "new"));
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> FieldUtils.writeStaticField(StaticContainerChild.class, "IMMUTABLE_PRIVATE", "new"));
}
@Test
@@ -990,78 +759,45 @@ public class FieldUtilsTest {
assertEquals("new", StaticContainer.getMutablePackage());
FieldUtils.writeStaticField(StaticContainerChild.class, "mutablePrivate", "new", true);
assertEquals("new", StaticContainer.getMutablePrivate());
- try {
- FieldUtils.writeStaticField(StaticContainerChild.class, "IMMUTABLE_PUBLIC", "new", true);
- fail("Expected IllegalAccessException");
- } catch (final IllegalAccessException e) {
- // pass
- }
- try {
- FieldUtils.writeStaticField(StaticContainerChild.class, "IMMUTABLE_PROTECTED", "new", true);
- fail("Expected IllegalAccessException");
- } catch (final IllegalAccessException e) {
- // pass
- }
- try {
- FieldUtils.writeStaticField(StaticContainerChild.class, "IMMUTABLE_PACKAGE", "new", true);
- fail("Expected IllegalAccessException");
- } catch (final IllegalAccessException e) {
- // pass
- }
- try {
- FieldUtils.writeStaticField(StaticContainerChild.class, "IMMUTABLE_PRIVATE", "new", true);
- fail("Expected IllegalAccessException");
- } catch (final IllegalAccessException e) {
- // pass
- }
+ assertThrows(
+ IllegalAccessException.class,
+ () -> FieldUtils.writeStaticField(StaticContainerChild.class, "IMMUTABLE_PUBLIC", "new", true));
+ assertThrows(
+ IllegalAccessException.class,
+ () -> FieldUtils.writeStaticField(StaticContainerChild.class, "IMMUTABLE_PROTECTED", "new", true));
+ assertThrows(
+ IllegalAccessException.class,
+ () -> FieldUtils.writeStaticField(StaticContainerChild.class, "IMMUTABLE_PACKAGE", "new", true));
+ assertThrows(
+ IllegalAccessException.class,
+ () -> FieldUtils.writeStaticField(StaticContainerChild.class, "IMMUTABLE_PRIVATE", "new", true));
}
@Test
public void testWriteDeclaredNamedStaticField() throws Exception {
FieldUtils.writeStaticField(StaticContainer.class, "mutablePublic", "new");
assertEquals("new", StaticContainer.mutablePublic);
- try {
- FieldUtils.writeDeclaredStaticField(StaticContainer.class, "mutableProtected", "new");
- fail("Expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // pass
- }
- try {
- FieldUtils.writeDeclaredStaticField(StaticContainer.class, "mutablePackage", "new");
- fail("Expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // pass
- }
- try {
- FieldUtils.writeDeclaredStaticField(StaticContainer.class, "mutablePrivate", "new");
- fail("Expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // pass
- }
- try {
- FieldUtils.writeDeclaredStaticField(StaticContainer.class, "IMMUTABLE_PUBLIC", "new");
- fail("Expected IllegalAccessException");
- } catch (final IllegalAccessException e) {
- // pass
- }
- try {
- FieldUtils.writeDeclaredStaticField(StaticContainer.class, "IMMUTABLE_PROTECTED", "new");
- fail("Expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // pass
- }
- try {
- FieldUtils.writeDeclaredStaticField(StaticContainer.class, "IMMUTABLE_PACKAGE", "new");
- fail("Expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // pass
- }
- try {
- FieldUtils.writeDeclaredStaticField(StaticContainer.class, "IMMUTABLE_PRIVATE", "new");
- fail("Expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // pass
- }
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> FieldUtils.writeDeclaredStaticField(StaticContainer.class, "mutableProtected", "new"));
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> FieldUtils.writeDeclaredStaticField(StaticContainer.class, "mutablePackage", "new"));
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> FieldUtils.writeDeclaredStaticField(StaticContainer.class, "mutablePrivate", "new"));
+ assertThrows(
+ IllegalAccessException.class,
+ () -> FieldUtils.writeDeclaredStaticField(StaticContainer.class, "IMMUTABLE_PUBLIC", "new"));
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> FieldUtils.writeDeclaredStaticField(StaticContainer.class, "IMMUTABLE_PROTECTED", "new"));
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> FieldUtils.writeDeclaredStaticField(StaticContainer.class, "IMMUTABLE_PACKAGE", "new"));
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> FieldUtils.writeDeclaredStaticField(StaticContainer.class, "IMMUTABLE_PRIVATE", "new"));
}
@Test
@@ -1074,30 +810,18 @@ public class FieldUtilsTest {
assertEquals("new", StaticContainer.getMutablePackage());
FieldUtils.writeDeclaredStaticField(StaticContainer.class, "mutablePrivate", "new", true);
assertEquals("new", StaticContainer.getMutablePrivate());
- try {
- FieldUtils.writeDeclaredStaticField(StaticContainer.class, "IMMUTABLE_PUBLIC", "new", true);
- fail("Expected IllegalAccessException");
- } catch (final IllegalAccessException e) {
- // pass
- }
- try {
- FieldUtils.writeDeclaredStaticField(StaticContainer.class, "IMMUTABLE_PROTECTED", "new", true);
- fail("Expected IllegalAccessException");
- } catch (final IllegalAccessException e) {
- // pass
- }
- try {
- FieldUtils.writeDeclaredStaticField(StaticContainer.class, "IMMUTABLE_PACKAGE", "new", true);
- fail("Expected IllegalAccessException");
- } catch (final IllegalAccessException e) {
- // pass
- }
- try {
- FieldUtils.writeDeclaredStaticField(StaticContainer.class, "IMMUTABLE_PRIVATE", "new", true);
- fail("Expected IllegalAccessException");
- } catch (final IllegalAccessException e) {
- // pass
- }
+ assertThrows(
+ IllegalAccessException.class,
+ () -> FieldUtils.writeDeclaredStaticField(StaticContainer.class, "IMMUTABLE_PUBLIC", "new", true));
+ assertThrows(
+ IllegalAccessException.class,
+ () -> FieldUtils.writeDeclaredStaticField(StaticContainer.class, "IMMUTABLE_PROTECTED", "new", true));
+ assertThrows(
+ IllegalAccessException.class,
+ () -> FieldUtils.writeDeclaredStaticField(StaticContainer.class, "IMMUTABLE_PACKAGE", "new", true));
+ assertThrows(
+ IllegalAccessException.class,
+ () -> FieldUtils.writeDeclaredStaticField(StaticContainer.class, "IMMUTABLE_PRIVATE", "new", true));
}
@Test
@@ -1105,27 +829,15 @@ public class FieldUtilsTest {
Field field = parentClass.getDeclaredField("s");
FieldUtils.writeField(field, publicChild, "S");
assertEquals("S", field.get(publicChild));
- field = parentClass.getDeclaredField("b");
- try {
- FieldUtils.writeField(field, publicChild, Boolean.TRUE);
- fail("Expected IllegalAccessException");
- } catch (final IllegalAccessException e) {
- // pass
- }
- field = parentClass.getDeclaredField("i");
- try {
- FieldUtils.writeField(field, publicChild, Integer.valueOf(Integer.MAX_VALUE));
- fail("Expected IllegalAccessException");
- } catch (final IllegalAccessException e) {
- // pass
- }
- field = parentClass.getDeclaredField("d");
- try {
- FieldUtils.writeField(field, publicChild, Double.valueOf(Double.MAX_VALUE));
- fail("Expected IllegalAccessException");
- } catch (final IllegalAccessException e) {
- // pass
- }
+ assertThrows(
+ IllegalAccessException.class,
+ () -> FieldUtils.writeField(parentClass.getDeclaredField("b"), publicChild, Boolean.TRUE));
+ assertThrows(
+ IllegalAccessException.class,
+ () -> FieldUtils.writeField(parentClass.getDeclaredField("i"), publicChild, Integer.valueOf(Integer.MAX_VALUE)));
+ assertThrows(
+ IllegalAccessException.class,
+ () -> FieldUtils.writeField(parentClass.getDeclaredField("d"), publicChild, Double.valueOf(Double.MAX_VALUE)));
}
@Test
@@ -1148,24 +860,10 @@ public class FieldUtilsTest {
public void testWriteNamedField() throws Exception {
FieldUtils.writeField(publicChild, "s", "S");
assertEquals("S", FieldUtils.readField(publicChild, "s"));
- try {
- FieldUtils.writeField(publicChild, "b", Boolean.TRUE);
- fail("Expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // pass
- }
- try {
- FieldUtils.writeField(publicChild, "i", Integer.valueOf(1));
- fail("Expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // pass
- }
- try {
- FieldUtils.writeField(publicChild, "d", Double.valueOf(1.0));
- fail("Expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // pass
- }
+ assertThrows(IllegalArgumentException.class, () -> FieldUtils.writeField(publicChild, "b", Boolean.TRUE));
+ assertThrows(IllegalArgumentException.class, () -> FieldUtils.writeField(publicChild, "i", Integer.valueOf(1)));
+ assertThrows(
+ IllegalArgumentException.class, () -> FieldUtils.writeField(publicChild, "d", Double.valueOf(1.0)));
FieldUtils.writeField(publiclyShadowedChild, "s", "S");
assertEquals("S", FieldUtils.readField(publiclyShadowedChild, "s"));
@@ -1178,24 +876,15 @@ public class FieldUtilsTest {
FieldUtils.writeField(privatelyShadowedChild, "s", "S");
assertEquals("S", FieldUtils.readField(privatelyShadowedChild, "s"));
- try {
- FieldUtils.writeField(privatelyShadowedChild, "b", Boolean.TRUE);
- fail("Expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // pass
- }
- try {
- FieldUtils.writeField(privatelyShadowedChild, "i", Integer.valueOf(1));
- fail("Expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // pass
- }
- try {
- FieldUtils.writeField(privatelyShadowedChild, "d", Double.valueOf(1.0));
- fail("Expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // pass
- }
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> FieldUtils.writeField(privatelyShadowedChild, "b", Boolean.TRUE));
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> FieldUtils.writeField(privatelyShadowedChild, "i", Integer.valueOf(1)));
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> FieldUtils.writeField(privatelyShadowedChild, "d", Double.valueOf(1.0)));
}
@Test
@@ -1230,30 +919,15 @@ public class FieldUtilsTest {
@Test
public void testWriteDeclaredNamedField() throws Exception {
- try {
- FieldUtils.writeDeclaredField(publicChild, "s", "S");
- fail("Expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // pass
- }
- try {
- FieldUtils.writeDeclaredField(publicChild, "b", Boolean.TRUE);
- fail("Expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // pass
- }
- try {
- FieldUtils.writeDeclaredField(publicChild, "i", Integer.valueOf(1));
- fail("Expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // pass
- }
- try {
- FieldUtils.writeDeclaredField(publicChild, "d", Double.valueOf(1.0));
- fail("Expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // pass
- }
+ assertThrows(IllegalArgumentException.class, () -> FieldUtils.writeDeclaredField(publicChild, "s", "S"));
+ assertThrows(
+ IllegalArgumentException.class, () -> FieldUtils.writeDeclaredField(publicChild, "b", Boolean.TRUE));
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> FieldUtils.writeDeclaredField(publicChild, "i", Integer.valueOf(1)));
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> FieldUtils.writeDeclaredField(publicChild, "d", Double.valueOf(1.0)));
FieldUtils.writeDeclaredField(publiclyShadowedChild, "s", "S");
assertEquals("S", FieldUtils.readDeclaredField(publiclyShadowedChild, "s"));
@@ -1264,58 +938,31 @@ public class FieldUtilsTest {
FieldUtils.writeDeclaredField(publiclyShadowedChild, "d", Double.valueOf(0.0));
assertEquals(Double.valueOf(0.0), FieldUtils.readDeclaredField(publiclyShadowedChild, "d"));
- try {
- FieldUtils.writeDeclaredField(privatelyShadowedChild, "s", "S");
- fail("Expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // pass
- }
- try {
- FieldUtils.writeDeclaredField(privatelyShadowedChild, "b", Boolean.TRUE);
- fail("Expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // pass
- }
- try {
- FieldUtils.writeDeclaredField(privatelyShadowedChild, "i", Integer.valueOf(1));
- fail("Expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // pass
- }
- try {
- FieldUtils.writeDeclaredField(privatelyShadowedChild, "d", Double.valueOf(1.0));
- fail("Expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // pass
- }
+ assertThrows(
+ IllegalArgumentException.class, () -> FieldUtils.writeDeclaredField(privatelyShadowedChild, "s", "S"));
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> FieldUtils.writeDeclaredField(privatelyShadowedChild, "b", Boolean.TRUE));
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> FieldUtils.writeDeclaredField(privatelyShadowedChild, "i", Integer.valueOf(1)));
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> FieldUtils.writeDeclaredField(privatelyShadowedChild, "d", Double.valueOf(1.0)));
}
@Test
public void testWriteDeclaredNamedFieldForceAccess() throws Exception {
- try {
- FieldUtils.writeDeclaredField(publicChild, "s", "S", true);
- fail("Expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // pass
- }
- try {
- FieldUtils.writeDeclaredField(publicChild, "b", Boolean.TRUE, true);
- fail("Expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // pass
- }
- try {
- FieldUtils.writeDeclaredField(publicChild, "i", Integer.valueOf(1), true);
- fail("Expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // pass
- }
- try {
- FieldUtils.writeDeclaredField(publicChild, "d", Double.valueOf(1.0), true);
- fail("Expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // pass
- }
+ assertThrows(IllegalArgumentException.class, () -> FieldUtils.writeDeclaredField(publicChild, "s", "S", true));
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> FieldUtils.writeDeclaredField(publicChild, "b", Boolean.TRUE, true));
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> FieldUtils.writeDeclaredField(publicChild, "i", Integer.valueOf(1), true));
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> FieldUtils.writeDeclaredField(publicChild, "d", Double.valueOf(1.0), true));
FieldUtils.writeDeclaredField(publiclyShadowedChild, "s", "S", true);
assertEquals("S", FieldUtils.readDeclaredField(publiclyShadowedChild, "s", true));
http://git-wip-us.apache.org/repos/asf/commons-lang/blob/3387734b/src/test/java/org/apache/commons/lang3/reflect/MethodUtilsTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/lang3/reflect/MethodUtilsTest.java b/src/test/java/org/apache/commons/lang3/reflect/MethodUtilsTest.java
index 702473b..b0f4907 100644
--- a/src/test/java/org/apache/commons/lang3/reflect/MethodUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/reflect/MethodUtilsTest.java
@@ -28,7 +28,6 @@ import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.junit.jupiter.api.Assertions.fail;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
@@ -435,12 +434,7 @@ public class MethodUtilsTest {
assertEquals("foo(long...)", MethodUtils.invokeMethod(testBean, "foo",
1L, 2L));
- try {
- MethodUtils.invokeMethod(testBean, "foo",
- 1, 2);
- fail("should throw NoSuchMethodException");
- } catch (final NoSuchMethodException expected) {
- }
+ assertThrows(NoSuchMethodException.class, () -> MethodUtils.invokeMethod(testBean, "foo", 1, 2));
TestBean.verify(new ImmutablePair<>("String...", new String[]{"x", "y"}),
MethodUtils.invokeMethod(testBean, "varOverloadEcho", "x", "y"));
@@ -471,23 +465,14 @@ public class MethodUtilsTest {
"foo", new Object[]{NumberUtils.DOUBLE_ONE},
new Class[]{Double.TYPE}));
- try {
- MethodUtils
- .invokeExactMethod(testBean, "foo", NumberUtils.BYTE_ONE);
- fail("should throw NoSuchMethodException");
- } catch (final NoSuchMethodException e) {
- }
- try {
- MethodUtils
- .invokeExactMethod(testBean, "foo", NumberUtils.LONG_ONE);
- fail("should throw NoSuchMethodException");
- } catch (final NoSuchMethodException e) {
- }
- try {
- MethodUtils.invokeExactMethod(testBean, "foo", Boolean.TRUE);
- fail("should throw NoSuchMethodException");
- } catch (final NoSuchMethodException e) {
- }
+ assertThrows(
+ NoSuchMethodException.class,
+ () -> MethodUtils.invokeExactMethod(testBean, "foo", NumberUtils.BYTE_ONE));
+
+ assertThrows(
+ NoSuchMethodException.class,
+ () -> MethodUtils.invokeExactMethod(testBean, "foo", NumberUtils.LONG_ONE));
+ assertThrows(NoSuchMethodException.class, () -> MethodUtils.invokeExactMethod(testBean, "foo", Boolean.TRUE));
}
@Test
@@ -526,11 +511,8 @@ public class MethodUtilsTest {
TestBean.verify(new ImmutablePair<>("Number...", new Number[]{17, 23, 42}),
MethodUtils.invokeStaticMethod(TestBean.class, "varOverloadEchoStatic", 17, 23, 42));
- try {
- MethodUtils.invokeStaticMethod(TestBean.class, "does_not_exist");
- fail("should throw NoSuchMethodException");
- } catch (final NoSuchMethodException e) {
- }
+ assertThrows(
+ NoSuchMethodException.class, () -> MethodUtils.invokeStaticMethod(TestBean.class, "does_not_exist"));
}
@Test
@@ -551,24 +533,15 @@ public class MethodUtilsTest {
TestBean.class, "bar", new Object[]{NumberUtils.DOUBLE_ONE},
new Class[]{Double.TYPE}));
- try {
- MethodUtils.invokeExactStaticMethod(TestBean.class, "bar",
- NumberUtils.BYTE_ONE);
- fail("should throw NoSuchMethodException");
- } catch (final NoSuchMethodException e) {
- }
- try {
- MethodUtils.invokeExactStaticMethod(TestBean.class, "bar",
- NumberUtils.LONG_ONE);
- fail("should throw NoSuchMethodException");
- } catch (final NoSuchMethodException e) {
- }
- try {
- MethodUtils.invokeExactStaticMethod(TestBean.class, "bar",
- Boolean.TRUE);
- fail("should throw NoSuchMethodException");
- } catch (final NoSuchMethodException e) {
- }
+ assertThrows(
+ NoSuchMethodException.class,
+ () -> MethodUtils.invokeExactStaticMethod(TestBean.class, "bar", NumberUtils.BYTE_ONE));
+ assertThrows(
+ NoSuchMethodException.class,
+ () -> MethodUtils.invokeExactStaticMethod(TestBean.class, "bar", NumberUtils.LONG_ONE));
+ assertThrows(
+ NoSuchMethodException.class,
+ () -> MethodUtils.invokeExactStaticMethod(TestBean.class, "bar", Boolean.TRUE));
}
@Test
http://git-wip-us.apache.org/repos/asf/commons-lang/blob/3387734b/src/test/java/org/apache/commons/lang3/text/StrBuilderAppendInsertTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/lang3/text/StrBuilderAppendInsertTest.java b/src/test/java/org/apache/commons/lang3/text/StrBuilderAppendInsertTest.java
index 8421bd4..86a1b5c 100644
--- a/src/test/java/org/apache/commons/lang3/text/StrBuilderAppendInsertTest.java
+++ b/src/test/java/org/apache/commons/lang3/text/StrBuilderAppendInsertTest.java
@@ -20,7 +20,7 @@ package org.apache.commons.lang3.text;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.fail;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import java.text.DecimalFormatSymbols;
import java.util.Arrays;
@@ -165,47 +165,36 @@ public class StrBuilderAppendInsertTest {
sb.append("foo", 0, 3);
assertEquals("foo", sb.toString());
- try {
- sb.append("bar", -1, 1);
- fail("append(char[], -1,) expected IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
-
- try {
- sb.append("bar", 3, 1);
- fail("append(char[], 3,) expected IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
-
- try {
- sb.append("bar", 1, -1);
- fail("append(char[],, -1) expected IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
-
- try {
- sb.append("bar", 1, 3);
- fail("append(char[], 1, 3) expected IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
-
- try {
- sb.append("bar", -1, 3);
- fail("append(char[], -1, 3) expected IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
-
- try {
- sb.append("bar", 4, 0);
- fail("append(char[], 4, 0) expected IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
+ final StrBuilder sb1 = sb;
+ assertThrows(
+ IndexOutOfBoundsException.class,
+ () -> sb1.append("bar", -1, 1),
+ "append(char[], -1,) expected IndexOutOfBoundsException");
+
+ assertThrows(
+ IndexOutOfBoundsException.class,
+ () -> sb1.append("bar", 3, 1),
+ "append(char[], 3,) expected IndexOutOfBoundsException");
+
+ assertThrows(
+ IndexOutOfBoundsException.class,
+ () -> sb1.append("bar", 1, -1),
+ "append(char[],, -1) expected IndexOutOfBoundsException");
+
+ assertThrows(
+ IndexOutOfBoundsException.class,
+ () -> sb1.append("bar", 1, 3),
+ "append(char[], 1, 3) expected IndexOutOfBoundsException");
+
+ assertThrows(
+ IndexOutOfBoundsException.class,
+ () -> sb1.append("bar", -1, 3),
+ "append(char[], -1, 3) expected IndexOutOfBoundsException");
+
+ assertThrows(
+ IndexOutOfBoundsException.class,
+ () -> sb1.append("bar", 4, 0),
+ "append(char[], 4, 0) expected IndexOutOfBoundsException");
sb.append("bar", 3, 0);
assertEquals("foo", sb.toString());
@@ -228,47 +217,36 @@ public class StrBuilderAppendInsertTest {
sb.append(new StringBuilder("foo"), 0, 3);
assertEquals("foo", sb.toString());
- try {
- sb.append(new StringBuilder("bar"), -1, 1);
- fail("append(StringBuilder, -1,) expected IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
-
- try {
- sb.append(new StringBuilder("bar"), 3, 1);
- fail("append(StringBuilder, 3,) expected IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
-
- try {
- sb.append(new StringBuilder("bar"), 1, -1);
- fail("append(StringBuilder,, -1) expected IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
-
- try {
- sb.append(new StringBuilder("bar"), 1, 3);
- fail("append(StringBuilder, 1, 3) expected IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
-
- try {
- sb.append(new StringBuilder("bar"), -1, 3);
- fail("append(StringBuilder, -1, 3) expected IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
-
- try {
- sb.append(new StringBuilder("bar"), 4, 0);
- fail("append(StringBuilder, 4, 0) expected IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
+ final StrBuilder sb1 = sb;
+ assertThrows(
+ IndexOutOfBoundsException.class,
+ () -> sb1.append(new StringBuilder("bar"), -1, 1),
+ "append(StringBuilder, -1,) expected IndexOutOfBoundsException");
+
+ assertThrows(
+ IndexOutOfBoundsException.class,
+ () -> sb1.append(new StringBuilder("bar"), 3, 1),
+ "append(StringBuilder, 3,) expected IndexOutOfBoundsException");
+
+ assertThrows(
+ IndexOutOfBoundsException.class,
+ () -> sb1.append(new StringBuilder("bar"), 1, -1),
+ "append(StringBuilder,, -1) expected IndexOutOfBoundsException");
+
+ assertThrows(
+ IndexOutOfBoundsException.class,
+ () -> sb1.append(new StringBuilder("bar"), 1, 3),
+ "append(StringBuilder, 1, 3) expected IndexOutOfBoundsException");
+
+ assertThrows(
+ IndexOutOfBoundsException.class,
+ () -> sb1.append(new StringBuilder("bar"), -1, 3),
+ "append(StringBuilder, -1, 3) expected IndexOutOfBoundsException");
+
+ assertThrows(
+ IndexOutOfBoundsException.class,
+ () -> sb1.append(new StringBuilder("bar"), 4, 0),
+ "append(StringBuilder, 4, 0) expected IndexOutOfBoundsException");
sb.append(new StringBuilder("bar"), 3, 0);
assertEquals("foo", sb.toString());
@@ -309,47 +287,36 @@ public class StrBuilderAppendInsertTest {
sb.append(new StringBuffer("foo"), 0, 3);
assertEquals("foo", sb.toString());
- try {
- sb.append(new StringBuffer("bar"), -1, 1);
- fail("append(char[], -1,) expected IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
-
- try {
- sb.append(new StringBuffer("bar"), 3, 1);
- fail("append(char[], 3,) expected IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
-
- try {
- sb.append(new StringBuffer("bar"), 1, -1);
- fail("append(char[],, -1) expected IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
-
- try {
- sb.append(new StringBuffer("bar"), 1, 3);
- fail("append(char[], 1, 3) expected IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
-
- try {
- sb.append(new StringBuffer("bar"), -1, 3);
- fail("append(char[], -1, 3) expected IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
-
- try {
- sb.append(new StringBuffer("bar"), 4, 0);
- fail("append(char[], 4, 0) expected IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
+ final StrBuilder sb1 = sb;
+ assertThrows(
+ IndexOutOfBoundsException.class,
+ () -> sb1.append(new StringBuffer("bar"), -1, 1),
+ "append(char[], -1,) expected IndexOutOfBoundsException");
+
+ assertThrows(
+ IndexOutOfBoundsException.class,
+ () -> sb1.append(new StringBuffer("bar"), 3, 1),
+ "append(char[], 3,) expected IndexOutOfBoundsException");
+
+ assertThrows(
+ IndexOutOfBoundsException.class,
+ () -> sb1.append(new StringBuffer("bar"), 1, -1),
+ "append(char[],, -1) expected IndexOutOfBoundsException");
+
+ assertThrows(
+ IndexOutOfBoundsException.class,
+ () -> sb1.append(new StringBuffer("bar"), 1, 3),
+ "append(char[], 1, 3) expected IndexOutOfBoundsException");
+
+ assertThrows(
+ IndexOutOfBoundsException.class,
+ () -> sb1.append(new StringBuffer("bar"), -1, 3),
+ "append(char[], -1, 3) expected IndexOutOfBoundsException");
+
+ assertThrows(
+ IndexOutOfBoundsException.class,
+ () -> sb1.append(new StringBuffer("bar"), 4, 0),
+ "append(char[], 4, 0) expected IndexOutOfBoundsException");
sb.append(new StringBuffer("bar"), 3, 0);
assertEquals("foo", sb.toString());
@@ -387,47 +354,36 @@ public class StrBuilderAppendInsertTest {
sb.append(new StrBuilder("foo"), 0, 3);
assertEquals("foo", sb.toString());
- try {
- sb.append(new StrBuilder("bar"), -1, 1);
- fail("append(char[], -1,) expected IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
-
- try {
- sb.append(new StrBuilder("bar"), 3, 1);
- fail("append(char[], 3,) expected IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
-
- try {
- sb.append(new StrBuilder("bar"), 1, -1);
- fail("append(char[],, -1) expected IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
-
- try {
- sb.append(new StrBuilder("bar"), 1, 3);
- fail("append(char[], 1, 3) expected IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
-
- try {
- sb.append(new StrBuilder("bar"), -1, 3);
- fail("append(char[], -1, 3) expected IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
-
- try {
- sb.append(new StrBuilder("bar"), 4, 0);
- fail("append(char[], 4, 0) expected IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
+ final StrBuilder sb1 = sb;
+ assertThrows(
+ IndexOutOfBoundsException.class,
+ () -> sb1.append(new StrBuilder("bar"), -1, 1),
+ "append(char[], -1,) expected IndexOutOfBoundsException");
+
+ assertThrows(
+ IndexOutOfBoundsException.class,
+ () -> sb1.append(new StrBuilder("bar"), 3, 1),
+ "append(char[], 3,) expected IndexOutOfBoundsException");
+
+ assertThrows(
+ IndexOutOfBoundsException.class,
+ () -> sb1.append(new StrBuilder("bar"), 1, -1),
+ "append(char[],, -1) expected IndexOutOfBoundsException");
+
+ assertThrows(
+ IndexOutOfBoundsException.class,
+ () -> sb1.append(new StrBuilder("bar"), 1, 3),
+ "append(char[], 1, 3) expected IndexOutOfBoundsException");
+
+ assertThrows(
+ IndexOutOfBoundsException.class,
+ () -> sb1.append(new StrBuilder("bar"), -1, 3),
+ "append(char[], -1, 3) expected IndexOutOfBoundsException");
+
+ assertThrows(
+ IndexOutOfBoundsException.class,
+ () -> sb1.append(new StrBuilder("bar"), 4, 0),
+ "append(char[], 4, 0) expected IndexOutOfBoundsException");
sb.append(new StrBuilder("bar"), 3, 0);
assertEquals("foo", sb.toString());
@@ -462,47 +418,36 @@ public class StrBuilderAppendInsertTest {
sb.append(new char[]{'f', 'o', 'o'}, 0, 3);
assertEquals("foo", sb.toString());
- try {
- sb.append(new char[]{'b', 'a', 'r'}, -1, 1);
- fail("append(char[], -1,) expected IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
-
- try {
- sb.append(new char[]{'b', 'a', 'r'}, 3, 1);
- fail("append(char[], 3,) expected IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
-
- try {
- sb.append(new char[]{'b', 'a', 'r'}, 1, -1);
- fail("append(char[],, -1) expected IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
-
- try {
- sb.append(new char[]{'b', 'a', 'r'}, 1, 3);
- fail("append(char[], 1, 3) expected IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
-
- try {
- sb.append(new char[]{'b', 'a', 'r'}, -1, 3);
- fail("append(char[], -1, 3) expected IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
-
- try {
- sb.append(new char[]{'b', 'a', 'r'}, 4, 0);
- fail("append(char[], 4, 0) expected IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
+ final StrBuilder sb1 = sb;
+ assertThrows(
+ IndexOutOfBoundsException.class,
+ () -> sb1.append(new char[]{'b', 'a', 'r'}, -1, 1),
+ "append(char[], -1,) expected IndexOutOfBoundsException");
+
+ assertThrows(
+ IndexOutOfBoundsException.class,
+ () -> sb1.append(new char[]{'b', 'a', 'r'}, 3, 1),
+ "append(char[], 3,) expected IndexOutOfBoundsException");
+
+ assertThrows(
+ IndexOutOfBoundsException.class,
+ () -> sb1.append(new char[]{'b', 'a', 'r'}, 1, -1),
+ "append(char[],, -1) expected IndexOutOfBoundsException");
+
+ assertThrows(
+ IndexOutOfBoundsException.class,
+ () -> sb1.append(new char[]{'b', 'a', 'r'}, 1, 3),
+ "append(char[], 1, 3) expected IndexOutOfBoundsException");
+
+ assertThrows(
+ IndexOutOfBoundsException.class,
+ () -> sb1.append(new char[]{'b', 'a', 'r'}, -1, 3),
+ "append(char[], -1, 3) expected IndexOutOfBoundsException");
+
+ assertThrows(
+ IndexOutOfBoundsException.class,
+ () -> sb1.append(new char[]{'b', 'a', 'r'}, 4, 0),
+ "append(char[], 4, 0) expected IndexOutOfBoundsException");
sb.append(new char[]{'b', 'a', 'r'}, 3, 0);
assertEquals("foo", sb.toString());
@@ -1288,19 +1233,15 @@ public class StrBuilderAppendInsertTest {
sb.append("barbaz");
assertEquals("barbaz", sb.toString());
- try {
- sb.insert(-1, FOO);
- fail("insert(-1, Object) expected StringIndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
+ assertThrows(
+ IndexOutOfBoundsException.class,
+ () -> sb.insert(-1, FOO),
+ "insert(-1, Object) expected StringIndexOutOfBoundsException");
- try {
- sb.insert(7, FOO);
- fail("insert(7, Object) expected StringIndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
+ assertThrows(
+ IndexOutOfBoundsException.class,
+ () -> sb.insert(7, FOO),
+ "insert(7, Object) expected StringIndexOutOfBoundsException");
sb.insert(0, (Object) null);
assertEquals("barbaz", sb.toString());
@@ -1312,19 +1253,15 @@ public class StrBuilderAppendInsertTest {
sb.append("barbaz");
assertEquals("barbaz", sb.toString());
- try {
- sb.insert(-1, "foo");
- fail("insert(-1, String) expected StringIndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
+ assertThrows(
+ IndexOutOfBoundsException.class,
+ () -> sb.insert(-1, "foo"),
+ "insert(-1, String) expected StringIndexOutOfBoundsException");
- try {
- sb.insert(7, "foo");
- fail("insert(7, String) expected StringIndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
+ assertThrows(
+ IndexOutOfBoundsException.class,
+ () -> sb.insert(7, "foo"),
+ "insert(7, String) expected StringIndexOutOfBoundsException");
sb.insert(0, (String) null);
assertEquals("barbaz", sb.toString());
@@ -1336,19 +1273,15 @@ public class StrBuilderAppendInsertTest {
sb.append("barbaz");
assertEquals("barbaz", sb.toString());
- try {
- sb.insert(-1, new char[]{'f', 'o', 'o'});
- fail("insert(-1, char[]) expected StringIndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
+ assertThrows(
+ IndexOutOfBoundsException.class,
+ () -> sb.insert(-1, new char[]{'f', 'o', 'o'}),
+ "insert(-1, char[]) expected StringIndexOutOfBoundsException");
- try {
- sb.insert(7, new char[]{'f', 'o', 'o'});
- fail("insert(7, char[]) expected StringIndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
+ assertThrows(
+ IndexOutOfBoundsException.class,
+ () -> sb.insert(7, new char[]{'f', 'o', 'o'}),
+ "insert(7, char[]) expected StringIndexOutOfBoundsException");
sb.insert(0, (char[]) null);
assertEquals("barbaz", sb.toString());
@@ -1363,19 +1296,15 @@ public class StrBuilderAppendInsertTest {
sb.append("barbaz");
assertEquals("barbaz", sb.toString());
- try {
- sb.insert(-1, new char[]{'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f'}, 3, 3);
- fail("insert(-1, char[], 3, 3) expected StringIndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
+ assertThrows(
+ IndexOutOfBoundsException.class,
+ () -> sb.insert(-1, new char[]{'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f'}, 3, 3),
+ "insert(-1, char[], 3, 3) expected StringIndexOutOfBoundsException");
- try {
- sb.insert(7, new char[]{'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f'}, 3, 3);
- fail("insert(7, char[], 3, 3) expected StringIndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
+ assertThrows(
+ IndexOutOfBoundsException.class,
+ () -> sb.insert(7, new char[]{'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f'}, 3, 3),
+ "insert(7, char[], 3, 3) expected StringIndexOutOfBoundsException");
sb.insert(0, null, 0, 0);
assertEquals("barbaz", sb.toString());
@@ -1383,33 +1312,25 @@ public class StrBuilderAppendInsertTest {
sb.insert(0, new char[0], 0, 0);
assertEquals("barbaz", sb.toString());
- try {
- sb.insert(0, new char[]{'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f'}, -1, 3);
- fail("insert(0, char[], -1, 3) expected StringIndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
+ assertThrows(
+ IndexOutOfBoundsException.class,
+ () -> sb.insert(0, new char[]{'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f'}, -1, 3),
+ "insert(0, char[], -1, 3) expected StringIndexOutOfBoundsException");
- try {
- sb.insert(0, new char[]{'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f'}, 10, 3);
- fail("insert(0, char[], 10, 3) expected StringIndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
+ assertThrows(
+ IndexOutOfBoundsException.class,
+ () -> sb.insert(0, new char[]{'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f'}, 10, 3),
+ "insert(0, char[], 10, 3) expected StringIndexOutOfBoundsException");
- try {
- sb.insert(0, new char[]{'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f'}, 0, -1);
- fail("insert(0, char[], 0, -1) expected StringIndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
+ assertThrows(
+ IndexOutOfBoundsException.class,
+ () -> sb.insert(0, new char[]{'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f'}, 0, -1),
+ "insert(0, char[], 0, -1) expected StringIndexOutOfBoundsException");
- try {
- sb.insert(0, new char[]{'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f'}, 0, 10);
- fail("insert(0, char[], 0, 10) expected StringIndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
+ assertThrows(
+ IndexOutOfBoundsException.class,
+ () -> sb.insert(0, new char[]{'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f'}, 0, 10),
+ "insert(0, char[], 0, 10) expected StringIndexOutOfBoundsException");
sb.insert(0, new char[]{'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f'}, 0, 0);
assertEquals("barbaz", sb.toString());
@@ -1421,19 +1342,15 @@ public class StrBuilderAppendInsertTest {
sb.append("barbaz");
assertEquals("barbaz", sb.toString());
- try {
- sb.insert(-1, true);
- fail("insert(-1, boolean) expected StringIndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
+ assertThrows(
+ IndexOutOfBoundsException.class,
+ () -> sb.insert(-1, true),
+ "insert(-1, boolean) expected StringIndexOutOfBoundsException");
- try {
- sb.insert(7, true);
- fail("insert(7, boolean) expected StringIndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
+ assertThrows(
+ IndexOutOfBoundsException.class,
+ () -> sb.insert(7, true),
+ "insert(7, boolean) expected StringIndexOutOfBoundsException");
sb.insert(0, true);
assertEquals("truebarbaz", sb.toString());
@@ -1445,19 +1362,15 @@ public class StrBuilderAppendInsertTest {
sb.append("barbaz");
assertEquals("barbaz", sb.toString());
- try {
- sb.insert(-1, '!');
- fail("insert(-1, char) expected StringIndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
+ assertThrows(
+ IndexOutOfBoundsException.class,
+ () -> sb.insert(-1, '!'),
+ "insert(-1, char) expected StringIndexOutOfBoundsException");
- try {
- sb.insert(7, '!');
- fail("insert(7, char) expected StringIndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
+ assertThrows(
+ IndexOutOfBoundsException.class,
+ () -> sb.insert(7, '!'),
+ "insert(7, char) expected StringIndexOutOfBoundsException");
sb.insert(0, '!');
assertEquals("!barbaz", sb.toString());
@@ -1466,19 +1379,15 @@ public class StrBuilderAppendInsertTest {
sb.append("barbaz");
assertEquals("barbaz", sb.toString());
- try {
- sb.insert(-1, 0);
- fail("insert(-1, int) expected StringIndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
+ assertThrows(
+ IndexOutOfBoundsException.class,
+ () -> sb.insert(-1, 0),
+ "insert(-1, int) expected StringIndexOutOfBoundsException");
- try {
- sb.insert(7, 0);
- fail("insert(7, int) expected StringIndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
+ assertThrows(
+ IndexOutOfBoundsException.class,
+ () -> sb.insert(7, 0),
+ "insert(7, int) expected StringIndexOutOfBoundsException");
sb.insert(0, '0');
assertEquals("0barbaz", sb.toString());
@@ -1487,19 +1396,15 @@ public class StrBuilderAppendInsertTest {
sb.append("barbaz");
assertEquals("barbaz", sb.toString());
- try {
- sb.insert(-1, 1L);
- fail("insert(-1, long) expected StringIndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
+ assertThrows(
+ IndexOutOfBoundsException.class,
+ () -> sb.insert(-1, 1L),
+ "insert(-1, long) expected StringIndexOutOfBoundsException");
- try {
- sb.insert(7, 1L);
- fail("insert(7, long) expected StringIndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
+ assertThrows(
+ IndexOutOfBoundsException.class,
+ () -> sb.insert(7, 1L),
+ "insert(7, long) expected StringIndexOutOfBoundsException");
sb.insert(0, 1L);
assertEquals("1barbaz", sb.toString());
@@ -1508,19 +1413,15 @@ public class StrBuilderAppendInsertTest {
sb.append("barbaz");
assertEquals("barbaz", sb.toString());
- try {
- sb.insert(-1, 2.3F);
- fail("insert(-1, float) expected StringIndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
+ assertThrows(
+ IndexOutOfBoundsException.class,
+ () -> sb.insert(-1, 2.3F),
+ "insert(-1, float) expected StringIndexOutOfBoundsException");
- try {
- sb.insert(7, 2.3F);
- fail("insert(7, float) expected StringIndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
+ assertThrows(
+ IndexOutOfBoundsException.class,
+ () -> sb.insert(7, 2.3F),
+ "insert(7, float) expected StringIndexOutOfBoundsException");
sb.insert(0, 2.3F);
assertEquals("2.3barbaz", sb.toString());
@@ -1529,19 +1430,15 @@ public class StrBuilderAppendInsertTest {
sb.append("barbaz");
assertEquals("barbaz", sb.toString());
- try {
- sb.insert(-1, 4.5D);
- fail("insert(-1, double) expected StringIndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
+ assertThrows(
+ IndexOutOfBoundsException.class,
+ () -> sb.insert(-1, 4.5D),
+ "insert(-1, double) expected StringIndexOutOfBoundsException");
- try {
- sb.insert(7, 4.5D);
- fail("insert(7, double) expected StringIndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
+ assertThrows(
+ IndexOutOfBoundsException.class,
+ () -> sb.insert(7, 4.5D),
+ "insert(7, double) expected StringIndexOutOfBoundsException");
sb.insert(0, 4.5D);
assertEquals("4.5barbaz", sb.toString());
@@ -1555,19 +1452,15 @@ public class StrBuilderAppendInsertTest {
sb.append("barbaz");
assertEquals("barbaz", sb.toString());
- try {
- sb.insert(-1, FOO);
- fail("insert(-1, Object) expected StringIndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
+ assertThrows(
+ IndexOutOfBoundsException.class,
+ () -> sb.insert(-1, FOO),
+ "insert(-1, Object) expected StringIndexOutOfBoundsException");
- try {
- sb.insert(7, FOO);
- fail("insert(7, Object) expected StringIndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
+ assertThrows(
+ IndexOutOfBoundsException.class,
+ () -> sb.insert(7, FOO),
+ "insert(7, Object) expected StringIndexOutOfBoundsException");
sb.insert(0, (Object) null);
assertEquals("nullbarbaz", sb.toString());
@@ -1579,19 +1472,15 @@ public class StrBuilderAppendInsertTest {
sb.append("barbaz");
assertEquals("barbaz", sb.toString());
- try {
- sb.insert(-1, "foo");
- fail("insert(-1, String) expected StringIndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
+ assertThrows(
+ IndexOutOfBoundsException.class,
+ () -> sb.insert(-1, "foo"),
+ "insert(-1, String) expected StringIndexOutOfBoundsException");
- try {
- sb.insert(7, "foo");
- fail("insert(7, String) expected StringIndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
+ assertThrows(
+ IndexOutOfBoundsException.class,
+ () -> sb.insert(7, "foo"),
+ "insert(7, String) expected StringIndexOutOfBoundsException");
sb.insert(0, (String) null);
assertEquals("nullbarbaz", sb.toString());