code updated
This commit is contained in:
parent
c745cf2e9b
commit
ff430ef23e
|
@ -22,12 +22,12 @@
|
|||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
|
||||
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
.classpath
|
||||
.project
|
||||
.settings
|
||||
|
||||
|
|
@ -1,9 +1,9 @@
|
|||
<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>com.sample</groupId>
|
||||
<groupId>org.baeldung</groupId>
|
||||
<artifactId>javaxval</artifactId>
|
||||
<version>1.0</version>
|
||||
<version>0.1-SNAPSHOT</version>
|
||||
|
||||
|
||||
<dependencies>
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package sample.model;
|
||||
package org.baeldung;
|
||||
|
||||
import javax.validation.constraints.AssertTrue;
|
||||
import javax.validation.constraints.Max;
|
|
@ -0,0 +1,66 @@
|
|||
package org.baeldung;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import javax.validation.ConstraintViolation;
|
||||
import javax.validation.Validation;
|
||||
import javax.validation.Validator;
|
||||
import javax.validation.ValidatorFactory;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ValidationTest {
|
||||
|
||||
@Test
|
||||
public void ifNameIsNull_nameValidationFails() {
|
||||
User user = new User();
|
||||
user.setWorking(true);
|
||||
user.setAboutMe("Its all about me!!");
|
||||
user.setAge(50);
|
||||
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<User>> violations = validator.validate(user);
|
||||
Assert.assertEquals(violations.isEmpty(), false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ifSizeNotInRange_aboutMeValidationFails() {
|
||||
User user = new User();
|
||||
user.setName("MyName");
|
||||
user.setAboutMe("Its all about me!!");
|
||||
user.setAge(50);
|
||||
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<User>> violations = validator.validate(user);
|
||||
Assert.assertEquals(violations.isEmpty(), false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ifWorkingIsFalse_workingValidationFails() {
|
||||
User user = new User();
|
||||
user.setName("MyName");
|
||||
user.setAboutMe("Its all about me!!");
|
||||
user.setAge(50);
|
||||
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<User>> violations = validator.validate(user);
|
||||
Assert.assertEquals(violations.isEmpty(), false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ifAgeNotRange_ageValidationFails() {
|
||||
User user = new User();
|
||||
user.setName("MyName");
|
||||
user.setAboutMe("Its all about me!!");
|
||||
user.setAge(8);
|
||||
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<User>> violations = validator.validate(user);
|
||||
Assert.assertEquals(violations.isEmpty(), false);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue