commit
dd1b419899
|
@ -0,0 +1,42 @@
|
|||
package org.baeldung.javaxval.messageinterpolator;
|
||||
|
||||
import javax.validation.constraints.Email;
|
||||
import javax.validation.constraints.Min;
|
||||
import javax.validation.constraints.Size;
|
||||
|
||||
public class Person {
|
||||
|
||||
@Size(min = 10, max = 100, message = "Name should be between {min} and {max} characters")
|
||||
private String name;
|
||||
|
||||
@Min(value = 18, message = "Age should not be less than {value}")
|
||||
private int age;
|
||||
|
||||
@Email(message = "Email address should be in a correct format: ${validatedValue}")
|
||||
private String email;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,70 @@
|
|||
package org.baeldung.javaxval.messageinterpolator;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import javax.validation.ConstraintViolation;
|
||||
import javax.validation.Validation;
|
||||
import javax.validation.Validator;
|
||||
import javax.validation.ValidatorFactory;
|
||||
|
||||
import org.hibernate.validator.messageinterpolation.ParameterMessageInterpolator;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ParameterMessageInterpolaterIntegrationTest {
|
||||
|
||||
private static Validator validator;
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeClass() {
|
||||
ValidatorFactory validatorFactory = Validation.byDefaultProvider()
|
||||
.configure()
|
||||
.messageInterpolator(new ParameterMessageInterpolator())
|
||||
.buildValidatorFactory();
|
||||
|
||||
validator = validatorFactory.getValidator();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNameLengthLessThanMin_whenValidate_thenValidationFails() {
|
||||
Person person = new Person();
|
||||
person.setName("John Doe");
|
||||
person.setAge(18);
|
||||
|
||||
Set<ConstraintViolation<Person>> violations = validator.validate(person);
|
||||
assertEquals(1, violations.size());
|
||||
|
||||
ConstraintViolation<Person> violation = violations.iterator().next();
|
||||
assertEquals("Name should be between 10 and 100 characters", violation.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAgeIsLessThanMin_whenValidate_thenValidationFails() {
|
||||
Person person = new Person();
|
||||
person.setName("John Stephaner Doe");
|
||||
person.setAge(16);
|
||||
|
||||
Set<ConstraintViolation<Person>> violations = validator.validate(person);
|
||||
assertEquals(1, violations.size());
|
||||
|
||||
ConstraintViolation<Person> violation = violations.iterator().next();
|
||||
assertEquals("Age should not be less than 18", violation.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmailIsMalformed_whenValidate_thenValidationFails() {
|
||||
Person person = new Person();
|
||||
person.setName("John Stephaner Doe");
|
||||
person.setAge(18);
|
||||
person.setEmail("johndoe.dev");
|
||||
|
||||
Set<ConstraintViolation<Person>> violations = validator.validate(person);
|
||||
assertEquals(1, violations.size());
|
||||
|
||||
ConstraintViolation<Person> violation = violations.iterator().next();
|
||||
assertEquals("Email address should be in a correct format: ${validatedValue}", violation.getMessage());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue