From d3f2a89ba229c57073e4f2a63a9a7f1053a5720d Mon Sep 17 00:00:00 2001 From: Benedikt Ritter Date: Thu, 6 Sep 2018 10:16:26 +0200 Subject: [PATCH] Convert tests for Validate.notNull overloads to @Nested test --- .../apache/commons/lang3/ValidateTest.java | 79 ++++++++++++------- 1 file changed, 50 insertions(+), 29 deletions(-) diff --git a/src/test/java/org/apache/commons/lang3/ValidateTest.java b/src/test/java/org/apache/commons/lang3/ValidateTest.java index c4cbe077f..2c1c6cb6c 100644 --- a/src/test/java/org/apache/commons/lang3/ValidateTest.java +++ b/src/test/java/org/apache/commons/lang3/ValidateTest.java @@ -139,39 +139,60 @@ void shouldThrowExceptionWithDoubleInsertedIntoTemplateMessageForFalseExpression } } - //----------------------------------------------------------------------- - //----------------------------------------------------------------------- - @SuppressWarnings("unused") - @Test - void testNotNull1() { - Validate.notNull(new Object()); - try { - Validate.notNull(null); - fail("Expecting NullPointerException"); - } catch (final NullPointerException ex) { - assertEquals("The validated object is null", ex.getMessage()); + @Nested + class NotNull { + + @Nested + class WithoutMessage { + + @Test + void shouldNotThrowForNonNullReference() { + Validate.notNull(new Object()); + } + + @Test + void shouldReturnTheSameInstance() { + final String str = "Hi"; + final String result = Validate.notNull(str); + + assertSame(str, result); + } + + @Test + void shouldThrowExceptionWithDefaultMessageForNullReference() { + final NullPointerException ex = assertThrows( + NullPointerException.class, + () -> Validate.notNull(null)); + + assertEquals("The validated object is null", ex.getMessage()); + } } - final String str = "Hi"; - final String testStr = Validate.notNull(str); - assertSame(str, testStr); - } + @Nested + class WithMessage { - //----------------------------------------------------------------------- - @SuppressWarnings("unused") - @Test - void testNotNull2() { - Validate.notNull(new Object(), "MSG"); - try { - Validate.notNull(null, "MSG"); - fail("Expecting NullPointerException"); - } catch (final NullPointerException ex) { - assertEquals("MSG", ex.getMessage()); + @Test + void shouldNotThrowForNonNullReference() { + Validate.notNull(new Object(), "MSG"); + } + + @Test + void shouldReturnTheSameInstance() { + final String str = "Hi"; + final String result = Validate.notNull(str, "MSG"); + + assertSame(str, result); + } + + @Test + void shouldThrowExceptionWithGivenMessageForNullReference() { + final NullPointerException ex = assertThrows( + NullPointerException.class, + () -> Validate.notNull(null, "MSG")); + + assertEquals("MSG", ex.getMessage()); + } } - - final String str = "Hi"; - final String testStr = Validate.notNull(str, "Message"); - assertSame(str, testStr); } //-----------------------------------------------------------------------