user7274818
user7274818

Reputation:

Annotation @NotEmpty doesn't check if String is null

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.

Upvotes: 5

Views: 11626

Answers (5)

Leon Deng
Leon Deng

Reputation: 49

Check the import for @NotEmpty :

org.hibernate.validator.constraints.NotEmpty

It works for me!

I use Spring Boot 2.5.5 and Hibernate validator 6.2.0.final.

Notice that javax.validation.constraints.NotBlank not work!

Upvotes: 1

user7274818
user7274818

Reputation:

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.

Upvotes: 0

ashit07
ashit07

Reputation: 49

@NotEmpty checks for @NotNull also and additionally checks the size/length of the provided object. For your above test case you will be getting 1 violation because you have not set the job name (its null) and you config says @NotEmpty(message = "mandatoryParametersMissing") private String name;

So @NotEmpty covers your @NotNull case.

I ran your above code and it worked fine. I used the below dependency

I ran your code and its working fine for me. Check the import for @NotEmpty

org.hibernate.validator.constraints.NotEmpty

I used the below dependency

   <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>6.0.13.Final</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish</groupId>
        <artifactId>javax.el</artifactId>
        <version>3.0.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/junit/junit -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>

Upvotes: 0

Martin van Wingerden
Martin van Wingerden

Reputation: 981

I created a demo-project with your code fragments and it works, could you maybe check for any differences? Did you really import javax.validation.constraints.NotEmpty.

Could you also share a demo-project which shows the actual problem? Could you maybe print the toString of the person object in your test and share the output of your

See demo-project

------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running nl.martinvw.test.PersonTest
jul 17, 2019 2:38:01 PM org.hibernate.validator.internal.util.Version <clinit>
INFO: HV000001: Hibernate Validator 6.1.0.Alpha5
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.353 sec

Results :

Tests run: 2, Failures: 0, Errors: 0, Skipped: 0

Upvotes: 0

user9065831
user9065831

Reputation:

@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."

Upvotes: 2

Related Questions