Testing javax validation

Overview

In this article we will see how to test the javax validation annotations.

Code

Let’s say we built a RESTful API where the client passes the payload in the POST request which we save in out database. Now before persisting in out DB we would like to check the values been passed. We can use javax.validation.constraints.

package com.meraj;

import javax.validation.constraints.AssertFalse;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

public class ValidationObject {

    @Size(min = 3, max = 10)
    private String size;

    @NotNull
    private String notNull;

    @AssertFalse
    private Boolean isFalse;

    public ValidationObject(String size, String notNull, Boolean isFalse) {
        this.size = size;
        this.notNull = notNull;
        this.isFalse = isFalse;
    }

    public String getSize() {
        return size;
    }

    public void setSize(String size) {
        this.size = size;
    }

    public String getNotNull() {
        return notNull;
    }

    public void setNotNull(String notNull) {
        this.notNull = notNull;
    }

    public Boolean getIsFalse() {
        return isFalse;
    }

    public void setIsFalse(Boolean isFalse) {
        this.isFalse = isFalse;
    }
}

Test

In this section we will see how to test the above code. We need a javax.validation.Validator class to do this.

private static Validator validator = Validation
            .byDefaultProvider()
            .configure()
            .messageInterpolator(new ParameterMessageInterpolator())
            .buildValidatorFactory()
            .getValidator();

Validation is the entry point for Bean Validation. Now we can call the validate() method on this validator object by passing the test object:

ValidationObject validationObject = buildValidationObject();
final Set<ConstraintViolation<ValidationObject>> violations = validator.validate(validationObject);

Full test method:

public class ValidationObjectTest {

    private static Validator validator = Validation
            .byDefaultProvider()
            .configure()
            .messageInterpolator(new ParameterMessageInterpolator())
            .buildValidatorFactory()
            .getValidator();

    @Test
    public void testValidation() {
        ValidationObject validationObject = buildValidationObject();
        final Set<ConstraintViolation<ValidationObject>> violations = validator.validate(validationObject);
        assertTrue(violations.isEmpty());
    }

    private ValidationObject buildValidationObject() {
        return new ValidationObject("12345", "not Null", false);
    }
}

Conclusion

In this article we saw how to test the javax.validation.constraints annotations. For the full source code you can check Javax Validation Testing

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: