Search in sources :

Example 46 with NotNullDef

use of org.hibernate.validator.cfg.defs.NotNullDef in project hibernate-validator by hibernate.

the class MethodConstraintMappingTest method constraintConfiguredOnFieldIsNotEvaluatedByMethodValidation.

@Test
public void constraintConfiguredOnFieldIsNotEvaluatedByMethodValidation() {
    ConstraintMapping mapping = config.createConstraintMapping();
    mapping.type(GreetingServiceImpl.class).field("hello").constraint(new NotNullDef());
    config.addMapping(mapping);
    GreetingService service = getValidatingProxy(wrappedObject, config.buildValidatorFactory().getValidator());
    assertNull(service.getHello());
}
Also used : ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) NotNullDef(org.hibernate.validator.cfg.defs.NotNullDef) Test(org.testng.annotations.Test)

Example 47 with NotNullDef

use of org.hibernate.validator.cfg.defs.NotNullDef in project hibernate-validator by hibernate.

the class MethodConstraintMappingTest method testCascadingMethodReturnDefinitionWithGroupConversion.

@Test
public void testCascadingMethodReturnDefinitionWithGroupConversion() {
    ConstraintMapping mapping = config.createConstraintMapping();
    mapping.type(GreetingService.class).method("greet", User.class).returnValue().valid().convertGroup(Default.class).to(TestGroup.class).type(Message.class).field("message").constraint(new NotNullDef().message("message must not be null").groups(TestGroup.class));
    config.addMapping(mapping);
    GreetingService service = getValidatingProxy(wrappedObject, config.buildValidatorFactory().getValidator());
    try {
        service.greet(new User("foo"));
        fail("Expected exception wasn't thrown.");
    } catch (ConstraintViolationException e) {
        assertThat(e.getConstraintViolations()).containsOnlyViolations(violationOf(NotNull.class).withMessage("message must not be null").withPropertyPath(pathWith().method("greet").returnValue().property("message")));
    }
}
Also used : ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) ConstraintViolationException(jakarta.validation.ConstraintViolationException) NotNullDef(org.hibernate.validator.cfg.defs.NotNullDef) Test(org.testng.annotations.Test)

Aggregations

NotNullDef (org.hibernate.validator.cfg.defs.NotNullDef)47 Test (org.testng.annotations.Test)42 ConstraintMapping (org.hibernate.validator.cfg.ConstraintMapping)37 HibernateValidator (org.hibernate.validator.HibernateValidator)31 Validator (jakarta.validation.Validator)28 TestForIssue (org.hibernate.validator.testutil.TestForIssue)22 ConstraintViolation (jakarta.validation.ConstraintViolation)19 ConstraintViolationException (jakarta.validation.ConstraintViolationException)10 NotNull (jakarta.validation.constraints.NotNull)9 HibernateValidatorConfiguration (org.hibernate.validator.HibernateValidatorConfiguration)4 NotEmptyDef (org.hibernate.validator.cfg.defs.NotEmptyDef)4 Test (org.junit.Test)4 SizeDef (org.hibernate.validator.cfg.defs.SizeDef)3 ExecutableValidator (jakarta.validation.executable.ExecutableValidator)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 MinDef (org.hibernate.validator.cfg.defs.MinDef)2