Convert tests for Validate.notBlank 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/c0779f42
Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/c0779f42
Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/c0779f42

Branch: refs/heads/master
Commit: c0779f42c7ca46c4cd3ade6261544b0da733e5d1
Parents: d784612
Author: Benedikt Ritter <brit...@apache.org>
Authored: Thu Sep 6 14:59:59 2018 +0200
Committer: Benedikt Ritter <brit...@apache.org>
Committed: Thu Sep 6 14:59:59 2018 +0200

----------------------------------------------------------------------
 .../org/apache/commons/lang3/ValidateTest.java  | 285 +++++++------------
 1 file changed, 99 insertions(+), 186 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-lang/blob/c0779f42/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 10b4c6c..e747fcf 100644
--- a/src/test/java/org/apache/commons/lang3/ValidateTest.java
+++ b/src/test/java/org/apache/commons/lang3/ValidateTest.java
@@ -28,7 +28,6 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
-import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -497,220 +496,134 @@ class ValidateTest {
         }
     }
 
-    //-----------------------------------------------------------------------
-    //-----------------------------------------------------------------------
-    @Test
-    void testNotBlankNullStringShouldThrow() {
-        //given
-        final String string = null;
-
-        try {
-            //when
-            Validate.notBlank(string);
-            fail("Expecting NullPointerException");
-        } catch (final NullPointerException e) {
-            //then
-            assertEquals("The validated character sequence is blank", 
e.getMessage());
-        }
-    }
+    @Nested
+    class NotBlank {
 
-    //-----------------------------------------------------------------------
-    @Test
-    void testNotBlankMsgNullStringShouldThrow() {
-        //given
-        final String string = null;
+        @Nested
+        class WithoutMessage {
 
-        try {
-            //when
-            Validate.notBlank(string, "Message");
-            fail("Expecting NullPointerException");
-        } catch (final NullPointerException e) {
-            //then
-            assertEquals("Message", e.getMessage());
-        }
-    }
+            @Test
+            void shouldNotThrowExceptionForNonEmptyString() {
+                Validate.notBlank("abc");
+            }
 
-    //-----------------------------------------------------------------------
-    @Test
-    void testNotBlankEmptyStringShouldThrow() {
-        //given
-        final String string = "";
+            @Test
+            void shouldNotThrowExceptionForNonEmptyStringContainingSpaces() {
+                Validate.notBlank("  abc   ");
+            }
 
-        try {
-            //when
-            Validate.notBlank(string);
-            fail("Expecting IllegalArgumentException");
-        } catch (final IllegalArgumentException e) {
-            //then
-            assertEquals("The validated character sequence is blank", 
e.getMessage());
-        }
-    }
+            @Test
+            void 
shouldNotThrowExceptionForNonEmptyStringContainingWhitespaceChars() {
+                Validate.notBlank(" \n \t abc \r \n ");
+            }
 
-    //-----------------------------------------------------------------------
-    @Test
-    void testNotBlankBlankStringWithWhitespacesShouldThrow() {
-        //given
-        final String string = "   ";
+            @Test
+            void shouldReturnNonBlankValue() {
+                final String str = "abc";
+                final String result = Validate.notBlank(str);
 
-        try {
-            //when
-            Validate.notBlank(string);
-            fail("Expecting IllegalArgumentException");
-        } catch (final IllegalArgumentException e) {
-            //then
-            assertEquals("The validated character sequence is blank", 
e.getMessage());
-        }
-    }
+                assertSame(str, result);
+            }
 
-    //-----------------------------------------------------------------------
-    @Test
-    void testNotBlankBlankStringWithNewlinesShouldThrow() {
-        //given
-        final String string = " \n \t \r \n ";
+            @Test
+            void 
shouldThrowNullPointerExceptionWithDefaultMessageForNullString() {
+                final NullPointerException ex = assertThrows(
+                        NullPointerException.class,
+                        () -> Validate.notBlank(null));
 
-        try {
-            //when
-            Validate.notBlank(string);
-            fail("Expecting IllegalArgumentException");
-        } catch (final IllegalArgumentException e) {
-            //then
-            assertEquals("The validated character sequence is blank", 
e.getMessage());
-        }
-    }
+                assertEquals("The validated character sequence is blank", 
ex.getMessage());
+            }
 
-    //-----------------------------------------------------------------------
-    @Test
-    void testNotBlankMsgBlankStringShouldThrow() {
-        //given
-        final String string = " \n \t \r \n ";
+            @Test
+            void 
shouldThrowIllegalArgumentExceptionWithDefaultMessageForEmptyString() {
+                final IllegalArgumentException ex = assertThrows(
+                        IllegalArgumentException.class,
+                        () -> Validate.notBlank(""));
 
-        try {
-            //when
-            Validate.notBlank(string, "Message");
-            fail("Expecting IllegalArgumentException");
-        } catch (final IllegalArgumentException e) {
-            //then
-            assertEquals("Message", e.getMessage());
-        }
-    }
+                assertEquals("The validated character sequence is blank", 
ex.getMessage());
+            }
 
-    //-----------------------------------------------------------------------
-    @Test
-    void testNotBlankMsgBlankStringWithWhitespacesShouldThrow() {
-        //given
-        final String string = "   ";
+            @Test
+            void 
shouldThrowIllegalArgumentExceptionWithDefaultMessageForBlankString() {
+                final IllegalArgumentException ex = assertThrows(
+                        IllegalArgumentException.class,
+                        () -> Validate.notBlank("   "));
 
-        try {
-            //when
-            Validate.notBlank(string, "Message");
-            fail("Expecting IllegalArgumentException");
-        } catch (final IllegalArgumentException e) {
-            //then
-            assertEquals("Message", e.getMessage());
-        }
-    }
+                assertEquals("The validated character sequence is blank", 
ex.getMessage());
+            }
 
-    //-----------------------------------------------------------------------
-    @Test
-    void testNotBlankMsgEmptyStringShouldThrow() {
-        //given
-        final String string = "";
+            @Test
+            void 
shouldThrowIllegalArgumentExceptionWithDefaultMessageForStringContainingOnlyWhitespaceChars()
 {
+                final IllegalArgumentException ex = assertThrows(
+                        IllegalArgumentException.class,
+                        () -> Validate.notBlank(" \n \t \r \n "));
 
-        try {
-            //when
-            Validate.notBlank(string, "Message");
-            fail("Expecting IllegalArgumentException");
-        } catch (final IllegalArgumentException e) {
-            //then
-            assertEquals("Message", e.getMessage());
+                assertEquals("The validated character sequence is blank", 
ex.getMessage());
+            }
         }
-    }
-
-    //-----------------------------------------------------------------------
-    @Test
-    void testNotBlankNotBlankStringShouldNotThrow() {
-        //given
-        final String string = "abc";
-
-        //when
-        Validate.notBlank(string);
-
-        //then should not throw
-    }
-
-    //-----------------------------------------------------------------------
-    @Test
-    void testNotBlankNotBlankStringWithWhitespacesShouldNotThrow() {
-        //given
-        final String string = "  abc   ";
-
-        //when
-        Validate.notBlank(string);
-
-        //then should not throw
-    }
 
-    //-----------------------------------------------------------------------
-    @Test
-    void testNotBlankNotBlankStringWithNewlinesShouldNotThrow() {
-        //given
-        final String string = " \n \t abc \r \n ";
+        @Nested
+        class WithMessage {
 
-        //when
-        Validate.notBlank(string);
+            @Test
+            void shouldNotThrowExceptionForNonEmptyString() {
+                Validate.notBlank("abc", "MSG");
+            }
 
-        //then should not throw
-    }
+            @Test
+            void shouldNotThrowExceptionForNonEmptyStringContainingSpaces() {
+                Validate.notBlank("  abc   ", "MSG");
+            }
 
-    //-----------------------------------------------------------------------
-    @Test
-    void testNotBlankMsgNotBlankStringShouldNotThrow() {
-        //given
-        final String string = "abc";
+            @Test
+            void 
shouldNotThrowExceptionForNonEmptyStringContainingWhitespaceChars() {
+                Validate.notBlank(" \n \t abc \r \n ", "MSG");
+            }
 
-        //when
-        Validate.notBlank(string, "Message");
+            @Test
+            void shouldReturnNonBlankValue() {
+                final String str = "abc";
+                final String result = Validate.notBlank(str, "MSG");
 
-        //then should not throw
-    }
+                assertSame(str, result);
+            }
 
-    //-----------------------------------------------------------------------
-    @Test
-    void testNotBlankMsgNotBlankStringWithWhitespacesShouldNotThrow() {
-        //given
-        final String string = "  abc   ";
+            @Test
+            void 
shouldThrowNullPointerExceptionWithGivenMessageForNullString() {
+                final NullPointerException ex = assertThrows(
+                        NullPointerException.class,
+                        () -> Validate.notBlank(null, "MSG"));
 
-        //when
-        Validate.notBlank(string, "Message");
+                assertEquals("MSG", ex.getMessage());
+            }
 
-        //then should not throw
-    }
+            @Test
+            void 
shouldThrowIllegalArgumentExceptionWithGivenMessageForEmptyString() {
+                final IllegalArgumentException ex = assertThrows(
+                        IllegalArgumentException.class,
+                        () -> Validate.notBlank("", "MSG"));
 
-    //-----------------------------------------------------------------------
-    @Test
-    void testNotBlankMsgNotBlankStringWithNewlinesShouldNotThrow() {
-        //given
-        final String string = " \n \t abc \r \n ";
+                assertEquals("MSG", ex.getMessage());
+            }
 
-        //when
-        Validate.notBlank(string, "Message");
+            @Test
+            void 
shouldThrowIllegalArgumentExceptionWithGivenMessageForBlankString() {
+                final IllegalArgumentException ex = assertThrows(
+                        IllegalArgumentException.class,
+                        () -> Validate.notBlank("   ", "MSG"));
 
-        //then should not throw
-    }
+                assertEquals("MSG", ex.getMessage());
+            }
 
-    //-----------------------------------------------------------------------
-    @Test
-    void testNotBlankReturnValues1() {
-        final String str = "Hi";
-        final String test = Validate.notBlank(str);
-        assertSame(str, test);
-    }
+            @Test
+            void 
shouldThrowIllegalArgumentExceptionWithGivenMessageForStringContainingOnlyWhitespaceChars()
 {
+                final IllegalArgumentException ex = assertThrows(
+                        IllegalArgumentException.class,
+                        () -> Validate.notBlank(" \n \t \r \n ", "MSG"));
 
-    @Test
-    void testNotBlankReturnValues2() {
-        final String str = "Hi";
-        final String test = Validate.notBlank(str, "Message");
-        assertSame(str, test);
+                assertEquals("MSG", ex.getMessage());
+            }
+        }
     }
 
     //-----------------------------------------------------------------------

Reply via email to