JSR 303 Bean ValidationViolationChecker

Introduction

In my daily work I often use Java Bean Validation (JSR 303) to get rid of validation boiler plate code in Java methods.

The problem

As Java Bean Validation is just declarative using annotations I want to test the validation in my JUnit tests. One may say these are integration tests instead of unit tests – anyway, I want to ensure I used the validation annotations correctly.

This normally is no problem, just declare a javax.validation.Validator and validate your object

Set<ConstraintViolation<User>> validationErrors = validator.validate(User.builder().build());

but what you get from the validator is a Set of ConstraintViolations which are a little bit unhandy to check against your expectations.

The solution – ValidationViolationChecker

After the third implementation of expectation checking against a Set of ConstraintViolations I decided to create my own little reusable tool, the ValidationViolationChecker.

The ValidationViolationChecker is a JUnit tool which is designed to be used in your JUnit tests and eases to check the expected validation violations.

General usage

You just need to instantiate somewhere in your JUnit test a new instance of ValidationViolationChecker with the type of your object, which needs to be validation. In the following example my object which needs to be validated is a custom User object.

private ValidationViolationChecker<User> userValidationChecker = new ValidationViolationChecker<>();

In your testcases the expected validation violations are checked like this

        // do the validation
Set<ConstraintViolation<User>> validationErrors = validator.validate(User.builder().build());

// do the validation expectation check
userValidationChecker.checkExpectedValidationViolations(
validationErrors, Arrays.asList(UserErrorMessages.NO_EMAIL,
UserErrorMessages.NO_LOGIN,
UserErrorMessages.NO_PASSWORD));

i18n and Validation messages

As everybody should know it is easily possible to internationalize the JSR 303 Validation messages. Does this have any effect on the ValidationViolationChecker?

The answer is no, the ValidationViolationChecker does not use the Validation messages but the Validation templates for checking the expected (or even unexpected) validation violations.

ValidationViolationChecker Maven dependency

The ValidationViolationChecker is available in the central maven repository, just add the dependency

<dependency>
<groupId>de.flexguse.util.junit</groupId>
<artifactId>validation-violation-checker</artifactId>
<version>0.1</version>
<scope>test</scope>
</dependency>

to your project and you should be ready to go.

ValidationViolationChecker source-code

ValidationViolationChecker is provided as OpenSource under the GNU software license. The sources are hosted on GitHub https://github.com/flexguse/validation-violation-checker.

Please use the GitHub issue tracker if you find any problem with the implementation.

ValidationViolationChecker demo application

I’m getting older and often I do not remember exactly how to use a technique or framework so it is always a good idea to have a little demo application with a minimal setup to demonstrate how the technique or framework works.

So I created a little demo application vor ValidationViolationChecker demonstrating how to check Validation Violations for simple object validation and for method argument validation in combination with the Spring 4.x framework.

The demo application is also hosted in GitHub https://github.com/flexguse/validation-violation-checker-demo as a simple Maven project.

Summary

The ValidationViolationChecker is a little and simple to use testing tool which might helps you to avoid writing some boiler plate code.

Happy testing!

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s