Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add additional tests to RequiredValidatorTest #448

Merged
merged 1 commit into from
Nov 16, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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<String> list) {
Expand Down Expand Up @@ -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
Expand Down