Unit / Integration Testing the Bean Validation (JSR-303)

First you will need to load both: the Bean Validation and EL SPIs. Hibernate Validator is the Reference Implementation (RI) for Bean Validation, and GlassFish implements the EL JSR:


Hibernate Validator requires Expression Language dependency. Without the javax.el dependency you will get the following error:

Caused by: javax.validation.ValidationException: HV000183: Unable to load 'javax.el.ExpressionFactory'. Check that you have the EL dependencies on the classpath
	at org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator.(ResourceBundleMessageInterpolator.java:172)
	at org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator.(ResourceBundleMessageInterpolator.java:118)

Now you are ready to launch your Integration Test:

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Test;

public class ValidationTest {

    private Validator validator;

    public void init() {

        ValidatorFactory vf = Validation.buildDefaultValidatorFactory();
        this.validator = vf.getValidator();


    public void prereqsMet() {
        Workshop validWorkshop = new Workshop(2, 2, true, 3);
        Set<ConstraintViolation<Workshop>> violations = this.validator.validate(validWorkshop);

Strictly speaking it is not a unit test, rather an Integration Test. In Unit Test you would like to test the validator logic only, without any dependencies to the SPI.

See you at Java EE Workshops at MUC Airport, especially at the Java EE 7 / Java 8 Testing day!

NEW online workshop: WebStandards Igniter (online)

Airport MUC workshops: Java EE 7: Bootstrap, Effective, Architectures, Web, React and Angular, Testing and Microservices

Podcast: airhacks.fm and newsletter: airhacks.news

A book about rethinking Java EE Patterns


Hi Adam,

Thanks for blogging about Bean Validation / Hibernate Validator!

In the upcoming 5.2 release of HV we will also provide an additional MessageInterpolator which does not require an implementation of the EL API (of course one cannot work with EL expressions then, but only the older {param} syntax known from BV 1.0).

As far as testing is concerned, we have some utilities (see https://github.com/hibernate/hibernate-validator/tree/master/engine/src/test/java/org/hibernate/validator/testutil) from which you may take inspiration for asserting constraint violation messages etc.


Posted by Gunnar on September 15, 2014 at 02:59 PM CEST #

Everything is just too confusing.I am not sure whether this all is necessessary for the use of Java.
Joerg A. Bahnemann

Posted by on October 30, 2014 at 08:32 AM CET #

Post a Comment:
  • HTML Syntax: NOT allowed
Online Workshops
...the last 150 posts
...the last 10 comments