Convert tests for Validate.isAssignable overloads to @Nested test

This commit is contained in:
Benedikt Ritter 2018-09-06 20:19:14 +02:00
parent 3e58ab33b9
commit 0b14928ee4
No known key found for this signature in database
GPG Key ID: 9DAADC1C9FCC82D0
1 changed files with 36 additions and 25 deletions

View File

@ -1843,32 +1843,43 @@ class ValidateTest {
} }
} }
@Test @Nested
void testIsAssignable() { class IsAssignable {
Validate.isAssignableFrom(CharSequence.class, String.class);
Validate.isAssignableFrom(AbstractList.class, ArrayList.class);
}
@Test @Nested
void testIsAssignableExceptionMessage() { class WithoutMessage {
try {
Validate.isAssignableFrom(List.class, String.class); @Test
fail("Expecting IllegalArgumentException"); void shouldNotThrowExceptionWhenClassIsAssignable() {
} catch (final IllegalArgumentException e) { Validate.isAssignableFrom(CharSequence.class, String.class);
assertEquals("Cannot assign a java.lang.String to a java.util.List", e.getMessage()); }
@Test
void shouldThrowIllegalArgumentExceptionWithDefaultMessageWhenClassIsNotAssignable() {
final IllegalArgumentException ex = assertThrows(
IllegalArgumentException.class,
() -> Validate.isAssignableFrom(List.class, String.class));
assertEquals("Cannot assign a java.lang.String to a java.util.List", ex.getMessage());
}
}
@Nested
class WithMessage {
@Test
void shouldNotThrowExceptionWhenClassIsAssignable() {
Validate.isAssignableFrom(CharSequence.class, String.class, "MSG");
}
@Test
void shouldThrowIllegalArgumentExceptionWithGiventMessageWhenClassIsNotAssignable() {
final IllegalArgumentException ex = assertThrows(
IllegalArgumentException.class,
() -> Validate.isAssignableFrom(List.class, String.class, "MSG"));
assertEquals("MSG", ex.getMessage());
}
} }
} }
@Test
void testIsAssignable_withMessage() {
Validate.isAssignableFrom(CharSequence.class, String.class, "Error");
Validate.isAssignableFrom(AbstractList.class, ArrayList.class, "Error");
try {
Validate.isAssignableFrom(List.class, String.class, "Error");
fail("Expecting IllegalArgumentException");
} catch (final IllegalArgumentException e) {
assertEquals("Error", e.getMessage());
}
}
} }