Multpile issues resolved
- fix formatting issues - add email field to Person - add email validation test
This commit is contained in:
@@ -1,16 +1,20 @@
|
||||
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 = 200, message = "Name should be in-between {min} and {max} characters")
|
||||
@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} but it is ${validatedValue}")
|
||||
@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;
|
||||
}
|
||||
@@ -27,4 +31,12 @@ public class Person {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ public class ParameterMessageInterpolaterIntegrationTest {
|
||||
assertEquals(1, violations.size());
|
||||
|
||||
ConstraintViolation<Person> violation = violations.iterator().next();
|
||||
assertEquals("Name should be in-between 10 and 200 characters", violation.getMessage());
|
||||
assertEquals("Name should be between 10 and 100 characters", violation.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -50,9 +50,21 @@ public class ParameterMessageInterpolaterIntegrationTest {
|
||||
assertEquals(1, violations.size());
|
||||
|
||||
ConstraintViolation<Person> violation = violations.iterator().next();
|
||||
assertEquals("Age should not be less than 18 but it is ${validatedValue}", violation.getMessage());
|
||||
assertEquals("Age should not be less than 18", violation.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ifEmailIsMalformed_emailFormatValidationFails() {
|
||||
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());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user