@Deployment
public static Archive<?> deployment() {
return ShrinkWrap.create(JavaArchive.class)
.addClasses(MyBean2.class, MyParameter.class)
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
}
cd javaee7-samples/validation/methods/Now we are ready to start testing. You can run all the tests in this sample by executing:
mvn test
Or you can run individual tests by executing one of the following:mvn test -Dtest=ConstructorParametersConstraintsTestmvn test -Dtest=ConstructorParametersInjectionTestmvn test -Dtest=MethodParametersConstraintsTest
@Deployment
public static Archive<?> deployment() {
return ShrinkWrap.create(JavaArchive.class)
.addClasses(MyBean2.class, MyParameter.class)
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
}
@Test
public void constructorViolationsWhenNullParameters() throws NoSuchMethodException, SecurityException {
final MyParameter parameter = new MyParameter();
ExecutableValidator methodValidator = validator.forExecutables();
Constructor<MyBean2> constructor = MyBean2.class
.getConstructor(parameter.getClass());
Set<ConstraintViolation<MyBean2>> constraints = methodValidator
.validateConstructorParameters(constructor, new Object[] {parameter});
ConstraintViolation<MyBean2> violation = constraints.iterator().next();
assertThat(constraints.size(), equalTo(1));
assertThat(violation.getMessageTemplate(), equalTo("{javax.validation.constraints.NotNull.message}"));
assertThat(violation.getPropertyPath().toString(), equalTo("MyBean2.arg0.value"));
}
@Test
public void constructorViolationsWhenNotNullParameters() throws NoSuchMethodException, SecurityException {
final MyParameter parameter = new MyParameter();
parameter.setValue("foo");
ExecutableValidator methodValidator = validator.forExecutables();
Constructor<MyBean2> constructor = MyBean2.class
.getConstructor(parameter.getClass());
Set<ConstraintViolation<MyBean2>> constraints = methodValidator
.validateConstructorParameters(constructor, new Object[] {parameter});
assertThat(constraints.isEmpty(), equalTo(true));
}
@Deployment
public static Archive<?> deployment() {
return ShrinkWrap.create(JavaArchive.class)
.addClasses(MyBean2.class, MyParameter.class)
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
}
@Test
public void constructorViolationsWhenNullParameters() {
thrown.expect(ConstraintViolationException.class);
thrown.expectMessage("javax.validation.constraints.NotNull");
thrown.expectMessage("MyBean2.arg0.value");
bean.getValue();
}
@Deployment
public static Archive<?> deployment() {
return ShrinkWrap.create(JavaArchive.class).addClasses(MyBean.class)
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
}
@Test
public void methodSizeTooLong() {
thrown.expect(ConstraintViolationException.class);
thrown.expectMessage("javax.validation.constraints.Size");
thrown.expectMessage("org.javaee7.validation.methods.MyBean.sayHello");
bean.sayHello("Duke");
}
@Test
public void methodSizeOk() {
bean.sayHello("Duk");
}
@Test
public void showDateFromPast() {
thrown.expect(ConstraintViolationException.class);
thrown.expectMessage("javax.validation.constraints.Future");
thrown.expectMessage("org.javaee7.validation.methods.MyBean.showDate");
bean.showDate(false);
}
@Test
public void showDateFromFuture() {
bean.showDate(true);
}
@Test
public void multipleParametersWithEmptyList() {
thrown.expect(ConstraintViolationException.class);
thrown.expectMessage("javax.validation.constraints.Size");
thrown.expectMessage("showList.arg0");
bean.showList(new ArrayList<String>(), "foo");
}
@Test
public void multipleParametersNullSecondParameter() {
thrown.expect(ConstraintViolationException.class);
thrown.expectMessage("javax.validation.constraints.NotNull");
thrown.expectMessage("showList.arg1");
List<String> list = new ArrayList<>();
list.add("bar");
bean.showList(list, null);
}
@Test
public void multipleParametersWithCorrectValues() {
List<String> list = new ArrayList<>();
list.add("bar");
list.add("woof");
String string = bean.showList(list, "foo");
assertThat(string, is(equalTo("foobar foowoof ")));
}
There's a lot more about JavaEE to cover. If you're ready to learn more, check out the other available samples.
git clone git://github.com/javaee-samples/javaee7-samples.git
cd javaee7-samples/validation/methods/
Do the changes as you see fit and send a pull request!
Good Luck!