I'm trying to validate some DTOs with javax.validation, but it seems that the annotation @NotEmpty doesn't check of the parameter is null.
Here are my clases:
Person.class
public class Person {
@NotEmpty(message = "mandatoryParametersMissing")
private String name;
@NotNull(message = "mandatoryParametersMissing")
@Valid
private Job job;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Job getJob() {
return job;
}
public void setJob(Job job) {
this.job = job;
}
@Override
public String toString() {
return "Person [name=" + name + ", job=" + job + "]";
}
}
Job.class
public class Job {
@NotEmpty(message = "mandatoryParametersMissing")
private String name;
@NotNull(message = "mandatoryParametersMissing")
private Integer salary;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getSalary() {
return salary;
}
public void setSalary(Integer salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Job [name=" + name + ", salary=" + salary + "]";
}
}
When I try to pass the following JUnit testS I get failures:
@Test(expected = BusinessServiceException.class)
public void testJobNameNull() {
Person samuel = new Person();
samuel.setName("Samuel Antequera");
Job programmer = new Job();
programmer.setSalary(18000);
samuel.setJob(programmer);
validatePerson(samuel);
}
And here's the method that validates the DTOs:
public void validatePerson(Person in) {
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<Person>> violations = validator.validate(in);
for (ConstraintViolation<Person> violation : violations) {
throw new BusinessServiceException(violation.getMessage(), violation.getPropertyPath().toString());
}
}
I was under the impression that @NotEmpty first checked if the parameter was null, ¿am I wrong?
PD: Here are the dependencies I use:
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.1.0.Alpha5</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator-annotation-processor</artifactId>
<version>6.1.0.Alpha5</version>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>3.0.1-b06</version>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>javax.el</artifactId>
<version>2.2.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
It seems the problem was with the dependencies, for some reason in the class path the jar version of validation-api was wrong I simply deleted all the jars in the class path and added them again and the error disappears.
@NotEmpty internally checks for minimum size of 1 and null.On documentation of @NotEmpty, it is clearly mentioned that :
"Asserts that the annotated string, collection, map or array is not {@code null} or empty."