You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by br...@apache.org on 2018/09/06 20:02:35 UTC

[13/16] [lang] Convert tests for Validate.isInstanceOf overloads to @Nested test

Convert tests for Validate.isInstanceOf overloads to @Nested test


Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-lang/commit/3e58ab33
Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/3e58ab33
Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/3e58ab33

Branch: refs/heads/master
Commit: 3e58ab33b9c294817699ce18277aa6e772d3ee4f
Parents: 5445f22
Author: Benedikt Ritter <br...@apache.org>
Authored: Thu Sep 6 20:14:29 2018 +0200
Committer: Benedikt Ritter <br...@apache.org>
Committed: Thu Sep 6 20:16:47 2018 +0200

----------------------------------------------------------------------
 .../org/apache/commons/lang3/ValidateTest.java  | 94 +++++++++++---------
 1 file changed, 50 insertions(+), 44 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-lang/blob/3e58ab33/src/test/java/org/apache/commons/lang3/ValidateTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/lang3/ValidateTest.java b/src/test/java/org/apache/commons/lang3/ValidateTest.java
index 418c301..3f5fe6a 100644
--- a/src/test/java/org/apache/commons/lang3/ValidateTest.java
+++ b/src/test/java/org/apache/commons/lang3/ValidateTest.java
@@ -1785,55 +1785,61 @@ class ValidateTest {
         }
     }
 
-    @Test
-    void testIsInstanceOf() {
-        Validate.isInstanceOf(String.class, "hi");
-        Validate.isInstanceOf(Integer.class, 1);
-    }
+    @Nested
+    class IsInstanceOf {
 
-    @Test
-    void testIsInstanceOfExceptionMessage() {
-        try {
-            Validate.isInstanceOf(List.class, "hi");
-            fail("Expecting IllegalArgumentException");
-        } catch (final IllegalArgumentException e) {
-            assertEquals("Expected type: java.util.List, actual: java.lang.String", e.getMessage());
-        }
-    }
+        @Nested
+        class WithoutMessage {
 
-    @Test
-    void testIsInstanceOf_withMessage() {
-        Validate.isInstanceOf(String.class, "hi", "Error");
-        Validate.isInstanceOf(Integer.class, 1, "Error");
-        try {
-            Validate.isInstanceOf(List.class, "hi", "Error");
-            fail("Expecting IllegalArgumentException");
-        } catch (final IllegalArgumentException e) {
-            assertEquals("Error", e.getMessage());
-        }
-    }
+            @Test
+            void shouldNotThrowExceptionWhenValueIsInstanceOfClass() {
+                Validate.isInstanceOf(String.class, "hi");
+            }
 
-    @Test
-    void testIsInstanceOf_withMessageArgs() {
-        Validate.isInstanceOf(String.class, "hi", "Error %s=%s", "Name", "Value");
-        Validate.isInstanceOf(Integer.class, 1, "Error %s=%s", "Name", "Value");
-        try {
-            Validate.isInstanceOf(List.class, "hi", "Error %s=%s", "Name", "Value");
-            fail("Expecting IllegalArgumentException");
-        } catch (final IllegalArgumentException e) {
-            assertEquals("Error Name=Value", e.getMessage());
+            @Test
+            void shouldThrowIllegalArgumentExceptionWithDefaultMessageWhenValueIsNotInstanceOfClass() {
+                final IllegalArgumentException ex = assertThrows(
+                        IllegalArgumentException.class,
+                        () -> Validate.isInstanceOf(List.class, "hi"));
+
+                assertEquals("Expected type: java.util.List, actual: java.lang.String", ex.getMessage());
+            }
         }
-        try {
-            Validate.isInstanceOf(List.class, "hi", "Error %s=%s", List.class, "Value");
-            fail("Expecting IllegalArgumentException");
-        } catch (final IllegalArgumentException e) {
-            assertEquals("Error interface java.util.List=Value", e.getMessage());
+
+        @Nested
+        class WithMessage {
+
+            @Test
+            void shouldNotThrowExceptionWhenValueIsInstanceOfClass() {
+                Validate.isInstanceOf(String.class, "hi", "MSG");
+            }
+
+            @Test
+            void shouldThrowIllegalArgumentExceptionWithGivenMessageWhenValueIsNotInstanceOfClass() {
+                final IllegalArgumentException ex = assertThrows(
+                        IllegalArgumentException.class,
+                        () -> Validate.isInstanceOf(List.class, "hi", "MSG"));
+
+                assertEquals("MSG", ex.getMessage());
+            }
         }
-        try {
-            Validate.isInstanceOf(List.class, "hi", "Error %s=%s", List.class, null);
-            fail("Expecting IllegalArgumentException");
-        } catch (final IllegalArgumentException e) {
-            assertEquals("Error interface java.util.List=null", e.getMessage());
+
+        @Nested
+        class WithMessageTemplate {
+
+            @Test
+            void shouldNotThrowExceptionWhenValueIsInstanceOfClass() {
+                Validate.isInstanceOf(String.class, "hi", "Error %s=%s", "Name", "Value");
+            }
+
+            @Test
+            void shouldThrowIllegalArgumentExceptionWithGivenMessageWhenValueIsNotInstanceOfClass() {
+                final IllegalArgumentException ex = assertThrows(
+                        IllegalArgumentException.class,
+                        () -> Validate.isInstanceOf(List.class, "hi", "Error %s=%s", "Name", "Value"));
+
+                assertEquals("Error Name=Value", ex.getMessage());
+            }
         }
     }