mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-15 12:15:50 +00:00
added a JUnit test for Bean Validation
This commit is contained in:
parent
193e7583f1
commit
c88e3d462a
3 changed files with 62 additions and 0 deletions
1
pom.xml
1
pom.xml
|
@ -248,6 +248,7 @@
|
|||
<artifactId>hibernate-validator</artifactId>
|
||||
<version>${hibernate-validator.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-ehcache</artifactId>
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
- DispatcherServlet application context for PetClinic's web tier.
|
||||
-->
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||
|
||||
<bean id="validatorFactory" class="javax.validation.Validation"
|
||||
factory-method="buildDefaultValidatorFactory" />
|
||||
|
||||
<bean id="validator" factory-bean="validatorFactory"
|
||||
factory-method="getValidator" />
|
||||
|
||||
</beans>
|
|
@ -0,0 +1,45 @@
|
|||
package org.springframework.samples.petclinic.model;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import javax.validation.ConstraintViolation;
|
||||
import javax.validation.Validator;
|
||||
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Michael Isvy
|
||||
* Simple test to make sure that Bean Validation is working
|
||||
* (useful when upgrading to a new version of Hibernate Validator/ Bean Validation)
|
||||
*
|
||||
*/
|
||||
@ContextConfiguration(locations = {"ValidatorTest-config.xml"})
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
public class ValidatorTest {
|
||||
|
||||
@Autowired
|
||||
private Validator validator;
|
||||
|
||||
@Test
|
||||
public void emptyFirstName() {
|
||||
|
||||
Person person = new Person();
|
||||
person.setFirstName("");
|
||||
person.setLastName("smith");
|
||||
|
||||
Set<ConstraintViolation<Person>> constraintViolations = validator.validate(person);
|
||||
|
||||
Assert.assertEquals(1, constraintViolations.size());
|
||||
ConstraintViolation<Person> violation = constraintViolations.iterator().next();
|
||||
Assert.assertEquals(violation.getPropertyPath().toString(), "firstName");
|
||||
Assert.assertEquals(violation.getMessage(), "may not be empty");
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue