Convert tests for Validate.matchesPattern overloads to @Nested test

This commit is contained in:
Benedikt Ritter 2018-09-06 18:14:59 +02:00
parent f6f8e5dbed
commit 8912be8a88
No known key found for this signature in database
GPG Key ID: 9DAADC1C9FCC82D0
1 changed files with 37 additions and 24 deletions

View File

@ -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);