From 8912be8a88781518e8e47d37a73d42a03a7e0e8e Mon Sep 17 00:00:00 2001 From: Benedikt Ritter Date: Thu, 6 Sep 2018 18:14:59 +0200 Subject: [PATCH] Convert tests for Validate.matchesPattern overloads to @Nested test --- .../apache/commons/lang3/ValidateTest.java | 61 +++++++++++-------- 1 file changed, 37 insertions(+), 24 deletions(-) diff --git a/src/test/java/org/apache/commons/lang3/ValidateTest.java b/src/test/java/org/apache/commons/lang3/ValidateTest.java index c6a089837..43519ea01 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()); + } + } + + @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 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()); - } - } - - //----------------------------------------------------------------------- - //----------------------------------------------------------------------- - @Test void testNotNaN1() { Validate.notNaN(0.0);