use of jakarta.validation.ConstraintViolationException in project hibernate-validator by hibernate.
the class FailFastTest method testFailFastMethodValidationSetOnValidatorFactory.
@Test
@TestForIssue(jiraKey = "HV-381")
public void testFailFastMethodValidationSetOnValidatorFactory() {
final HibernateValidatorConfiguration configuration = ValidatorUtil.getConfiguration(HibernateValidator.class);
final ValidatorFactory factory = configuration.buildValidatorFactory();
final Validator validator = factory.unwrap(HibernateValidatorFactory.class).usingContext().failFast(true).getValidator();
TestService service = getValidatingProxy(new TestServiceImpl(), validator);
try {
service.testMethod(" ", null);
fail();
} catch (ConstraintViolationException e) {
assertThat(e.getConstraintViolations()).containsOneOfViolations(violationOf(NotBlank.class), violationOf(Min.class));
}
}
use of jakarta.validation.ConstraintViolationException in project hibernate-validator by hibernate.
the class FailFastTest method testFailFastMethodValidationOnConfiguration.
@Test
@TestForIssue(jiraKey = "HV-381")
public void testFailFastMethodValidationOnConfiguration() {
final HibernateValidatorConfiguration configuration = ValidatorUtil.getConfiguration(HibernateValidator.class);
final ValidatorFactory factory = configuration.failFast(true).buildValidatorFactory();
final Validator validator = factory.getValidator();
TestService service = getValidatingProxy(new TestServiceImpl(), validator);
try {
service.testMethod("a", null);
fail();
} catch (ConstraintViolationException e) {
assertThat(e.getConstraintViolations()).containsOnlyViolations(violationOf(Min.class));
}
}
use of jakarta.validation.ConstraintViolationException in project hibernate-validator by hibernate.
the class FailFastTest method testFailFastMethodValidationSetWithProperty.
@Test
@TestForIssue(jiraKey = "HV-381")
public void testFailFastMethodValidationSetWithProperty() {
final HibernateValidatorConfiguration configuration = ValidatorUtil.getConfiguration(HibernateValidator.class);
final ValidatorFactory factory = configuration.addProperty(HibernateValidatorConfiguration.FAIL_FAST, "true").buildValidatorFactory();
final Validator validator = factory.getValidator();
TestService service = getValidatingProxy(new TestServiceImpl(), validator);
try {
service.testMethod(" ", null);
fail();
} catch (ConstraintViolationException e) {
assertThat(e.getConstraintViolations()).containsOneOfViolations(violationOf(NotBlank.class), violationOf(Min.class));
}
}
use of jakarta.validation.ConstraintViolationException in project hibernate-validator by hibernate.
the class MethodConstraintMappingTest method testParameterConstraint.
@Test
public void testParameterConstraint() {
ConstraintMapping mapping = config.createConstraintMapping();
mapping.type(GreetingService.class).method("greet", User.class).parameter(0).constraint(new NotNullDef());
config.addMapping(mapping);
try {
GreetingService service = getValidatingProxy(wrappedObject, config.buildValidatorFactory().getValidator());
service.greet((User) null);
fail("Expected exception wasn't thrown.");
} catch (ConstraintViolationException e) {
assertThat(e.getConstraintViolations()).containsOnlyViolations(violationOf(NotNull.class).withMessage("must not be null").withPropertyPath(pathWith().method("greet").parameter("user", 0)));
}
}
use of jakarta.validation.ConstraintViolationException in project hibernate-validator by hibernate.
the class MethodConstraintMappingTest method testOverridingMethodMayDefineSameConstraintsAsOverriddenMethod.
@Test
public void testOverridingMethodMayDefineSameConstraintsAsOverriddenMethod() {
ConstraintMapping mapping = config.createConstraintMapping();
mapping.type(GreetingService.class).method("greet", String.class).parameter(0).constraint(new SizeDef().min(5).max(10)).type(GreetingServiceImpl.class).method("greet", String.class).parameter(0).constraint(new SizeDef().min(5).max(10));
config.addMapping(mapping);
GreetingService service = getValidatingProxy(wrappedObject, config.buildValidatorFactory().getValidator());
try {
service.greet("Hi");
} catch (ConstraintViolationException e) {
assertThat(e.getConstraintViolations()).containsOnlyViolations(violationOf(Size.class).withMessage("size must be between 5 and 10").withPropertyPath(pathWith().method("greet").parameter("string", 0)));
}
}
Aggregations