Convert tests for Validate.matchesPattern 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/8912be8a Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/8912be8a Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/8912be8a Branch: refs/heads/master Commit: 8912be8a88781518e8e47d37a73d42a03a7e0e8e Parents: f6f8e5d Author: Benedikt Ritter <brit...@apache.org> Authored: Thu Sep 6 18:14:59 2018 +0200 Committer: Benedikt Ritter <brit...@apache.org> Committed: Thu Sep 6 18:14:59 2018 +0200 ---------------------------------------------------------------------- .../org/apache/commons/lang3/ValidateTest.java | 57 ++++++++++++-------- 1 file changed, 35 insertions(+), 22 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-lang/blob/8912be8a/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 c6a0898..43519ea 100644 --- a/src/test/java/org/apache/commons/lang3/ValidateTest.java +++ b/src/test/java/org/apache/commons/lang3/ValidateTest.java @@ -1067,33 +1067,46 @@ class ValidateTest { } } - @Test - void testMatchesPattern() { - final CharSequence str = "hi"; - Validate.matchesPattern(str, "[a-z]*"); - try { - Validate.matchesPattern(str, "[0-9]*"); - fail("Expecting IllegalArgumentException"); - } catch (final IllegalArgumentException e) { - assertEquals("The string hi does not match the pattern [0-9]*", e.getMessage()); + @Nested + class MatchesPattern { + + @Nested + class WithoutMessage { + + @Test + void shouldNotThrowExceptionWhenStringMatchesPattern() { + Validate.matchesPattern("hi", "[a-z]*"); + } + + @Test + void shouldThrowIllegalArgumentExceptionWithDefaultMessageWhenStringDoesNotMatchPattern() { + final IllegalArgumentException ex = assertThrows( + IllegalArgumentException.class, + () -> Validate.matchesPattern("hi", "[0-9]*")); + + assertEquals("The string hi does not match the pattern [0-9]*", ex.getMessage()); + } } - } - @Test - void testMatchesPattern_withMessage() { - final CharSequence str = "hi"; - Validate.matchesPattern(str, "[a-z]*", "Does not match"); - try { - Validate.matchesPattern(str, "[0-9]*", "Does not match"); - fail("Expecting IllegalArgumentException"); - } catch (final IllegalArgumentException e) { - assertEquals("Does not match", e.getMessage()); + @Nested + class WithMessage { + + @Test + void shouldNotThrowExceptionWhenStringMatchesPattern() { + Validate.matchesPattern("hi", "[a-z]*", "MSG"); + } + + @Test + void shouldThrowIllegalArgumentExceptionWhenStringDoesNotMatchPattern() { + final IllegalArgumentException ex = assertThrows( + IllegalArgumentException.class, + () -> Validate.matchesPattern("hi", "[0-9]*", "MSG")); + + assertEquals("MSG", ex.getMessage()); + } } } - //----------------------------------------------------------------------- - //----------------------------------------------------------------------- - @Test void testNotNaN1() { Validate.notNaN(0.0);