Hello JSR 349, but what is all with Red Hat and JBoss?
Some useful links first: this, this and this!
pom.xml
Employee.java
mymessages.properties
BaseValidatorTest.java
EmployeeTest.java
Some useful links first: this, this and this!
Sample Code
Directory Layout<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>biz.tugay</groupId> <artifactId>validation-examples</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>javax.validation</groupId> <artifactId>validation-api</artifactId> <version>1.1.0.Final</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>5.3.0.Final</version> </dependency> <dependency> <groupId>javax.el</groupId> <artifactId>javax.el-api</artifactId> <version>2.2.4</version> <scope>runtime</scope> </dependency> <dependency> <groupId>org.glassfish.web</groupId> <artifactId>javax.el</artifactId> <version>2.2.4</version> <scope>runtime</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> </project>
Employee.java
package biz.tugay.validation; import javax.validation.constraints.NotNull; /** * User: Koray Tugay (koray@tugay.biz) * Date: 11/4/2016 * Time: 7:44 PM */ public class Employee { @NotNull private String fullname; public Employee(final String fullname) { this.fullname = fullname; } @SuppressWarnings("unused") public String getFullname() { return fullname; } }
mymessages.properties
javax.validation.constraints.NotNull.message=This value can not be null!
BaseValidatorTest.java
package biz.tugay.validation; import org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator; import org.hibernate.validator.resourceloading.PlatformResourceBundleLocator; import javax.validation.Validation; import javax.validation.Validator; /** * User: Koray Tugay (koray@tugay.biz) * Date: 11/4/2016 * Time: 7:55 PM */ public abstract class BaseValidatorTest { // Validator must be cached! protected static Validator validator; static { validator = Validation.byDefaultProvider() .configure() .messageInterpolator( new ResourceBundleMessageInterpolator( new PlatformResourceBundleLocator("mymessages"))) .buildValidatorFactory() .getValidator(); } }
EmployeeTest.java
package biz.tugay.validation; import org.junit.Assert; import org.junit.Test; import javax.validation.ConstraintViolation; import java.util.Set; /** * User: Koray Tugay (koray@tugay.biz) * Date: 11/4/2016 * Time: 7:45 PM */ public class EmployeeTest extends BaseValidatorTest { @Test public void testEmployeeFullnameCannotBeNull() { final Employee mySampleObject = new Employee(null); final Set<ConstraintViolation<Employee>> constraintViolations = validator.validate(mySampleObject); Assert.assertTrue(constraintViolations.size() == 1); for (ConstraintViolation<Employee> constraintViolation : constraintViolations) { final String message = constraintViolation.getMessage(); System.out.println(constraintViolation.getMessage()); System.out.println(constraintViolation.getMessageTemplate()); Assert.assertTrue(message.equals("This value can not be null!")); } } @Test public void testEmployeeFullnameNotNull() { final Employee employee = new Employee("Koray Tugay"); final Set<ConstraintViolation<Employee>> constraintViolations = validator.validate(employee); Assert.assertTrue(constraintViolations.isEmpty()); } }