Adam Bien's Weblog

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:


	   <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.1.2.Final</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.glassfish</groupId>
            <artifactId>javax.el</artifactId>
            <version>3.0.0</version>
            <scope>test</scope>
        </dependency>

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;

    @Before
    public void init() {

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

    }

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

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 workshop: HTML 5 and JavaScript Essentials, Three days in April 2017: from Java EE 7 Architectures over Microservices to Performance, Troubleshooting and Monitoring
On demand workshops: Java EE 7: Bootstrap, Effective, Testing and Microservices available for streaming.

Newsletter: airhacks.news

A book about rethinking Java EE Patterns

Comments:

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.

--Gunnar

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 91.55.134.153 on October 30, 2014 at 08:32 AM CET #

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