Repository: commons-lang Updated Branches: refs/heads/master 3178494ca -> c3b38a3ba
Convert tests for Validate.isTrue overloads to @Nested test Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-lang/commit/aad2db8b Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/aad2db8b Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/aad2db8b Branch: refs/heads/master Commit: aad2db8b12b8c61556df9df7de4fadc927633504 Parents: bce28f9 Author: Benedikt Ritter <brit...@apache.org> Authored: Wed Sep 5 14:26:25 2018 +0200 Committer: Benedikt Ritter <brit...@apache.org> Committed: Wed Sep 5 14:26:25 2018 +0200 ---------------------------------------------------------------------- .../org/apache/commons/lang3/ValidateTest.java | 135 ++++++++++++------- 1 file changed, 86 insertions(+), 49 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-lang/blob/aad2db8b/src/test/java/org/apache/commons/lang3/ValidateTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/lang3/ValidateTest.java b/src/test/java/org/apache/commons/lang3/ValidateTest.java index 4d6113f..c4cbe07 100644 --- a/src/test/java/org/apache/commons/lang3/ValidateTest.java +++ b/src/test/java/org/apache/commons/lang3/ValidateTest.java @@ -18,6 +18,7 @@ */ package org.apache.commons.lang3; +import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import java.lang.reflect.Constructor; @@ -34,6 +35,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; @@ -42,63 +44,98 @@ import static org.junit.jupiter.api.Assertions.fail; */ class ValidateTest { - //----------------------------------------------------------------------- - @Test - void testIsTrue1() { - Validate.isTrue(true); - try { - Validate.isTrue(false); - fail("Expecting IllegalArgumentException"); - } catch (final IllegalArgumentException ex) { - assertEquals("The validated expression is false", ex.getMessage()); + @Nested + class IsTrue { + + @Nested + class WithoutMessage { + + @Test + void shouldNotThrowForTrueExpression() { + Validate.isTrue(true); + } + + @Test + void shouldThrowExceptionWithDefaultMessageForFalseExpression() { + final IllegalArgumentException ex = assertThrows( + IllegalArgumentException.class, + () -> Validate.isTrue(false)); + + assertEquals("The validated expression is false", ex.getMessage()); + } + } - } - //----------------------------------------------------------------------- - @Test - void testIsTrue2() { - Validate.isTrue(true, "MSG"); - try { - Validate.isTrue(false, "MSG"); - fail("Expecting IllegalArgumentException"); - } catch (final IllegalArgumentException ex) { - assertEquals("MSG", ex.getMessage()); + @Nested + class WithMessage { + + @Test + void shouldNotThrowForTrueExpression() { + Validate.isTrue(true, "MSG"); + } + + @Test + void shouldThrowExceptionWithGivenMessageForFalseExpression() { + final IllegalArgumentException ex = assertThrows( + IllegalArgumentException.class, + () -> Validate.isTrue(false, "MSG")); + + assertEquals("MSG", ex.getMessage()); + } } - } - //----------------------------------------------------------------------- - @Test - void testIsTrue3() { - Validate.isTrue(true, "MSG", 6); - try { - Validate.isTrue(false, "MSG", 6); - fail("Expecting IllegalArgumentException"); - } catch (final IllegalArgumentException ex) { - assertEquals("MSG", ex.getMessage()); + @Nested + class WithLongTemplate { + + @Test + void shouldNotThrowForTrueExpression() { + Validate.isTrue(true, "MSG", 6); + } + + @Test + void shouldThrowExceptionWithLongInsertedIntoTemplateMessageForFalseExpression() { + final IllegalArgumentException ex = assertThrows( + IllegalArgumentException.class, + () -> Validate.isTrue(false, "MSG %s", 6)); + + assertEquals("MSG 6", ex.getMessage()); + } } - } - //----------------------------------------------------------------------- - @Test - void testIsTrue4() { - Validate.isTrue(true, "MSG", 7); - try { - Validate.isTrue(false, "MSG", 7); - fail("Expecting IllegalArgumentException"); - } catch (final IllegalArgumentException ex) { - assertEquals("MSG", ex.getMessage()); + @Nested + class WithDoubleTemplate { + + @Test + void shouldNotThrowForTrueExpression() { + Validate.isTrue(true, "MSG", 7.4d); + } + + @Test + void shouldThrowExceptionWithDoubleInsertedIntoTemplateMessageForFalseExpression() { + final IllegalArgumentException ex = assertThrows( + IllegalArgumentException.class, + () -> Validate.isTrue(false, "MSG %s", 7.4d)); + + assertEquals("MSG 7.4", ex.getMessage()); + } } - } - //----------------------------------------------------------------------- - @Test - void testIsTrue5() { - Validate.isTrue(true, "MSG", 7.4d); - try { - Validate.isTrue(false, "MSG", 7.4d); - fail("Expecting IllegalArgumentException"); - } catch (final IllegalArgumentException ex) { - assertEquals("MSG", ex.getMessage()); + @Nested + class WithObjectTemplate { + + @Test + void shouldNotThrowForTrueExpression() { + Validate.isTrue(true, "MSG", "Object 1", "Object 2"); + } + + @Test + void shouldThrowExceptionWithDoubleInsertedIntoTemplateMessageForFalseExpression() { + final IllegalArgumentException ex = assertThrows( + IllegalArgumentException.class, + () -> Validate.isTrue(false, "MSG %s %s", "Object 1", "Object 2")); + + assertEquals("MSG Object 1 Object 2", ex.getMessage()); + } } }