From 0b14928ee447a59f69bcaa87051af62300c72c84 Mon Sep 17 00:00:00 2001 From: Benedikt Ritter Date: Thu, 6 Sep 2018 20:19:14 +0200 Subject: [PATCH] Convert tests for Validate.isAssignable overloads to @Nested test --- .../apache/commons/lang3/ValidateTest.java | 61 +++++++++++-------- 1 file changed, 36 insertions(+), 25 deletions(-) diff --git a/src/test/java/org/apache/commons/lang3/ValidateTest.java b/src/test/java/org/apache/commons/lang3/ValidateTest.java index 3f5fe6a8d..41033864e 100644 --- a/src/test/java/org/apache/commons/lang3/ValidateTest.java +++ b/src/test/java/org/apache/commons/lang3/ValidateTest.java @@ -1843,32 +1843,43 @@ class ValidateTest { } } - @Test - void testIsAssignable() { - Validate.isAssignableFrom(CharSequence.class, String.class); - Validate.isAssignableFrom(AbstractList.class, ArrayList.class); - } + @Nested + class IsAssignable { - @Test - void testIsAssignableExceptionMessage() { - try { - Validate.isAssignableFrom(List.class, String.class); - fail("Expecting IllegalArgumentException"); - } catch (final IllegalArgumentException e) { - assertEquals("Cannot assign a java.lang.String to a java.util.List", e.getMessage()); + @Nested + class WithoutMessage { + + @Test + void shouldNotThrowExceptionWhenClassIsAssignable() { + Validate.isAssignableFrom(CharSequence.class, String.class); + } + + @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()); - } - } - }