Convert tests for Validate.isInstanceOf overloads to @Nested test

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

View File

@ -1785,55 +1785,61 @@ void shouldThrowIllegalArgumentExceptionWithGivenMessageWhenValueIsAboveUpperBou
} }
} }
@Test @Nested
void testIsInstanceOf() { class IsInstanceOf {
Validate.isInstanceOf(String.class, "hi");
Validate.isInstanceOf(Integer.class, 1);
}
@Test @Nested
void testIsInstanceOfExceptionMessage() { class WithoutMessage {
try {
Validate.isInstanceOf(List.class, "hi");
fail("Expecting IllegalArgumentException");
} catch (final IllegalArgumentException e) {
assertEquals("Expected type: java.util.List, actual: java.lang.String", e.getMessage());
}
}
@Test @Test
void testIsInstanceOf_withMessage() { void shouldNotThrowExceptionWhenValueIsInstanceOfClass() {
Validate.isInstanceOf(String.class, "hi", "Error"); Validate.isInstanceOf(String.class, "hi");
Validate.isInstanceOf(Integer.class, 1, "Error"); }
try {
Validate.isInstanceOf(List.class, "hi", "Error");
fail("Expecting IllegalArgumentException");
} catch (final IllegalArgumentException e) {
assertEquals("Error", e.getMessage());
}
}
@Test @Test
void testIsInstanceOf_withMessageArgs() { void shouldThrowIllegalArgumentExceptionWithDefaultMessageWhenValueIsNotInstanceOfClass() {
Validate.isInstanceOf(String.class, "hi", "Error %s=%s", "Name", "Value"); final IllegalArgumentException ex = assertThrows(
Validate.isInstanceOf(Integer.class, 1, "Error %s=%s", "Name", "Value"); IllegalArgumentException.class,
try { () -> Validate.isInstanceOf(List.class, "hi"));
Validate.isInstanceOf(List.class, "hi", "Error %s=%s", "Name", "Value");
fail("Expecting IllegalArgumentException"); assertEquals("Expected type: java.util.List, actual: java.lang.String", ex.getMessage());
} catch (final IllegalArgumentException e) { }
assertEquals("Error Name=Value", e.getMessage());
} }
try {
Validate.isInstanceOf(List.class, "hi", "Error %s=%s", List.class, "Value"); @Nested
fail("Expecting IllegalArgumentException"); class WithMessage {
} catch (final IllegalArgumentException e) {
assertEquals("Error interface java.util.List=Value", e.getMessage()); @Test
void shouldNotThrowExceptionWhenValueIsInstanceOfClass() {
Validate.isInstanceOf(String.class, "hi", "MSG");
}
@Test
void shouldThrowIllegalArgumentExceptionWithGivenMessageWhenValueIsNotInstanceOfClass() {
final IllegalArgumentException ex = assertThrows(
IllegalArgumentException.class,
() -> Validate.isInstanceOf(List.class, "hi", "MSG"));
assertEquals("MSG", ex.getMessage());
}
} }
try {
Validate.isInstanceOf(List.class, "hi", "Error %s=%s", List.class, null); @Nested
fail("Expecting IllegalArgumentException"); class WithMessageTemplate {
} catch (final IllegalArgumentException e) {
assertEquals("Error interface java.util.List=null", e.getMessage()); @Test
void shouldNotThrowExceptionWhenValueIsInstanceOfClass() {
Validate.isInstanceOf(String.class, "hi", "Error %s=%s", "Name", "Value");
}
@Test
void shouldThrowIllegalArgumentExceptionWithGivenMessageWhenValueIsNotInstanceOfClass() {
final IllegalArgumentException ex = assertThrows(
IllegalArgumentException.class,
() -> Validate.isInstanceOf(List.class, "hi", "Error %s=%s", "Name", "Value"));
assertEquals("Error Name=Value", ex.getMessage());
}
} }
} }