mvn archetype:generate -DarchetypeArtifactId=maven-archetype-quickstart
[INFO] Generating project in Interactive mode
Define value for property 'groupId': : com.marcubus
Define value for property 'artifactId': : validation
Define value for property 'version': 1.0-SNAPSHOT: :
Define value for property 'package': com.marcubus: :
Edit pom.xml and add the following dependencies:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.2.0.Final</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.1.4.RELEASE</version>
<scope>test</scope>
</dependency>
The hibernate validator defines validation implementation of JSR-303 - these use the javax annotations for Java 6 validation. We want to use these to mark up the beans.
Also add the following build section - this will force the project use Java 8:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
While you're at it change the junit version to 4.11.
Create a bean to validate in the base package:
package com.marcubus;
public class Person {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Alright so - we've got a bean with no annotations - lets create a Validator and run a bean through it. Here is a test class - once run, the test should succeed.
package com.marcubus;
import static org.junit.Assert.assertEquals;
import java.util.Set;
import java.util.logging.Logger;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.ValidatorFactory;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class ValidationTest {
@Autowired
private ValidatorFactory validator;
private static Logger log = Logger.getLogger(Validation.class.getName());
private Person person;
@Configuration
static class ContextConfiguration {
@Bean(name="validator")
public ValidatorFactory getValidator() {
return new LocalValidatorFactoryBean();
}
}
@Before
public void setup() {
person = new Person();
}
@Test
public void validatorTest() {
Set<ConstraintViolation<Person>> result = validator.getValidator().validate(person);
result.forEach(c -> log.info(c.getPropertyPath() + " " + c.getMessage()));
assertEquals(0, result.size());
}
}
Looks happy. Now lets add some validation annotations to the Person bean and run the test again.
@NotNull
private String name;
The test should now fail and console should spit out:
INFO: name may not be null