Java Programming Tutorials

Java programming tutorials with many code examples!

Bean Validation Unit Testing

In this article we present practical Bean Validation Unit Testing using JUnit. Testing this part is very simple, yet often neglected by many developers. Let’s fix that!

Bean for validation

For testing purposes we’ll use the following Player class, which has a few default constraints on name and score:

package com.farenda.javax.validation;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.Size;

public class Player {

    // name have to be 3 chars:
    @Size(min = 3, max = 3)
    private String name;

    // possible score in game:
    @Min(0) @Max(100)
    private int score;

    public Player(String name, int score) {
        this.name = name;
        this.score = score;
    }

    // just for logs
    @Override
    public String toString() {
        return "Player{name='" + name + '\'' + ", score=" + score + '}';
    }
}

Create Bean Validator

The first thing we have to do is to create a Bean Validator. To do that we’ll instantiate a ValidatorFactory that will provide us a Validator before all tests:

package com.farenda.javax.validation;

import org.junit.AfterClass;
import org.junit.BeforeClass;

import javax.validation.Validator;
import javax.validation.ValidatorFactory;

public class PlayerValidationTest {
    private static ValidatorFactory validatorFactory;
    private static Validator validator;

    @BeforeClass
    public static void createValidator() {
        validatorFactory = Validation.buildDefaultValidatorFactory();
        validator = validatorFactory.getValidator();
    }

    //...

Both ValidatorFactory and Validator are thread-safe, so can be reused in many tests. You could even create an abstract test class for Bean Validation tests.
Also let’s dispose the ValidatorFactory after all tests to free any resources possibly allocated by the factory:

@AfterClass
public static void close() {
    validatorFactory.close();
}

Bean Validation Unit Test

Test no bean violations

The first test will exercise the simplest path, namely a valid bean:

@Test
public void shouldHaveNoViolations() {
    //given:
    Player player = new Player("ABC", 44);

    //when:
    Set<ConstraintViolation<Player>> violations
        = validator.validate(player);

    //then:
    assertTrue(violations.isEmpty());
}

Validator always returns non-null set of constraint violations.

Test constraint violation

Now we can unit test our constraints on Player’s name:

@Test
public void shouldDetectInvalidName() {
    //given too short name:
    Player player = new Player("a", 44);

    //when:
    Set<ConstraintViolation<Player>> violations
        = validator.validate(player);

    //then:
    assertEquals(violations.size(), 1);

    ConstraintViolation<Player> violation
        = violations.iterator().next();
    assertEquals("size must be between 3 and 3",
                 violation.getMessage());
    assertEquals("name", violation.getPropertyPath().toString());
    assertEquals("a", violation.getInvalidValue());
}

Complete JUnit Test

Here’s the complete test for easy copy-past:

package com.farenda.javax.validation;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import java.util.Set;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class PlayerValidationTest {

    private static ValidatorFactory validatorFactory;
    private static Validator validator;

    @BeforeClass
    public static void createValidator() {
        validatorFactory = Validation.buildDefaultValidatorFactory();
        validator = validatorFactory.getValidator();
    }

    @AfterClass
    public static void close() {
        validatorFactory.close();
    }

    @Test
    public void shouldHaveNoViolations() {
        //given:
        Player player = new Player("ABC", 44);

        //when:
        Set<ConstraintViolation<Player>> violations
                = validator.validate(player);

        //then:
        assertTrue(violations.isEmpty());
    }

    @Test
    public void shouldDetectInvalidName() {
        //given too short name:
        Player player = new Player("a", 44);

        //when:
        Set<ConstraintViolation<Player>> violations
                = validator.validate(player);

        //then:
        assertEquals(violations.size(), 1);

        ConstraintViolation<Player> violation
                = violations.iterator().next();
        assertEquals("size must be between 3 and 3",
                     violation.getMessage());
        assertEquals("name", violation.getPropertyPath().toString());
        assertEquals("a", violation.getInvalidValue());
    }
}

My impression is that many developers thing that testing validation is part of integration or even system testing and usually don’t do that. But here you can see that this really can be done on (almost) unit level and Bean Validation Unit Testing can done by developers together with other unit tests. Which is supercool! :-)

Share with the World!
  • Shaun McCready

    Hello,
    I have recently tried to add bean validation to my test and im running into an issue. I’ve done everything thats mentioned here and im getting the exception:
    java.lang.ClassNotFoundException: javax.el.PropertyNotFoundException

    I have the proper dependencies for this

    javax.el
    el-api
    2.2
    provided

    org.glassfish.web
    el-impl
    2.2

    So im not sure what the issue could be?