diff --git a/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/ObjectPropertyValidatorTests.java b/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/ObjectPropertyValidatorTests.java index 403be15..43c1684 100644 --- a/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/ObjectPropertyValidatorTests.java +++ b/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/ObjectPropertyValidatorTests.java @@ -18,6 +18,7 @@ package xyz.zhouxy.plusone.example.validator; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; import java.time.DateTimeException; @@ -44,12 +45,12 @@ public class ObjectPropertyValidatorTests { // ================================ @Test - void withRule() { + void withRule_validInput() { BaseValidator validator = new BaseValidator() { { ruleFor(ExampleCommand::getBoolProperty) .notNull("The boolProperty cannot be null.") - .withRule(Boolean.TRUE::equals, "The boolProperty should be true."); + .withRule(Boolean.TRUE::equals); ruleFor(ExampleCommand::getIntProperty) .withRule(intProperty -> intProperty > 0, "The intProperty should be greater than 0."); @@ -99,6 +100,50 @@ public class ObjectPropertyValidatorTests { assertDoesNotThrow(() -> validator.validate(command)); } + @Test + void withRule_invalidInputs() { + ExampleCommand command = new ExampleCommand(); + BaseValidator ruleWithDefaultMessage = new BaseValidator() { + { + ruleFor(ExampleCommand::getObjectProperty) + .withRule(x -> false); + } + }; + IllegalArgumentException eWithDefaultMessage = assertThrows( + IllegalArgumentException.class, () -> ruleWithDefaultMessage.validate(command)); + assertNull(eWithDefaultMessage.getMessage()); + + BaseValidator ruleWithMessage = new BaseValidator() { + { + ruleFor(ExampleCommand::getObjectProperty) + .withRule(x -> false, "invalid input."); + } + }; + IllegalArgumentException eWithMessage = assertThrows( + IllegalArgumentException.class, () -> ruleWithMessage.validate(command)); + assertEquals("invalid input.", eWithMessage.getMessage()); + + BaseValidator ruleWithExceptionSupplier = new BaseValidator() { + { + ruleFor(ExampleCommand::getObjectProperty) + .withRule(x -> false, () -> ExampleException.withMessage("invalid input.")); + } + }; + ExampleException specifiedException = assertThrows( + ExampleException.class, () -> ruleWithExceptionSupplier.validate(command)); + assertEquals("invalid input.", specifiedException.getMessage()); + + BaseValidator ruleWithExceptionFunction = new BaseValidator() { + { + ruleFor(ExampleCommand::getObjectProperty) + .withRule(x -> false, x -> ExampleException.withMessage("invalid input: [%s].", x)); + } + }; + ExampleException specifiedException2 = assertThrows( + ExampleException.class, () -> ruleWithExceptionFunction.validate(command)); + assertEquals("invalid input: [null].", specifiedException2.getMessage()); + } + // ================================ // #endregion - withRule // ================================