From 40e8f2b4100eaf5e8663da66a0e9522331ea5a24 Mon Sep 17 00:00:00 2001 From: Scott Leberknight <174812+sleberknight@users.noreply.github.com> Date: Mon, 16 Nov 2020 15:05:28 -0500 Subject: [PATCH] Add additional tests to RequiredValidatorTest (#448) --- .../validation/RequiredValidatorTest.java | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/test/java/org/kiwiproject/validation/RequiredValidatorTest.java b/src/test/java/org/kiwiproject/validation/RequiredValidatorTest.java index 6f8c6c7d..f33949af 100644 --- a/src/test/java/org/kiwiproject/validation/RequiredValidatorTest.java +++ b/src/test/java/org/kiwiproject/validation/RequiredValidatorTest.java @@ -15,6 +15,7 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.NullAndEmptySource; +import org.junit.jupiter.params.provider.ValueSource; import javax.validation.Validator; import java.util.HashMap; @@ -44,6 +45,13 @@ void shouldReturnExpectedErrorMessage() { @Nested class ValidatingStrings { + @ParameterizedTest + @ValueSource(strings = {"a", "aa", "a string"}) + void shouldValidateNonBlankStrings(String value) { + var obj = new RequiredDefaults(value); + assertNoPropertyViolations(validator, obj, "value"); + } + @ParameterizedTest @NullAndEmptySource void shouldRequireNonBlankOrEmptyString(String value) { @@ -67,6 +75,19 @@ void shouldPermitEmptyString() { @Nested class ValidatingCollections { + @Test + void shouldValidateNonEmptyCollections() { + var obj = HasCollectionsAndMaps.builder() + .requiredList(List.of("a", "b", "c")) + .requiredOrEmptyList(List.of(1, 2, 3)) + .requiredSet(Set.of("one", "two", "three")) + .requiredOrEmptySet(Set.of(4, 5, 6)) + .requiredMap(Map.of("a", 1, "b", 2, "c", 3)) + .requiredOrEmptyMap(Map.of("d", 4)) + .build(); + assertNoViolations(validator, obj); + } + @ParameterizedTest @NullAndEmptySource void shouldRequireNonEmptyList(List list) { @@ -130,10 +151,17 @@ void shouldAllowEmpty() { class WhenHasNoIsEmptyMethod { @Test - void shouldBeValid() { + void shouldBeValid_WhenGivenNonNullObject() { var person = new Person("Alice", new ContactInfo()); assertNoViolations(validator, person); } + + @Test + void shouldNotBeValid_WhenGivenNullObject() { + var person = new Person(); + assertOnePropertyViolation(validator, person, "name"); + assertOnePropertyViolation(validator, person, "contactInfo"); + } } @Nested