Methods

Run
How to run the sample
The source code for this sample can be found in the javaee7-samples GitHub repository. The first thing we need to do is to get the source by downloading the repository and then go into the samples folder:
git clone git://github.com/javaee-samples/javaee7-samples.git
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

ConstructorParametersConstraintsTest

Missing a description for the story. Add some javadoc to the TestClass. Show me how!
Missing a description for the deployment. Add some javadoc to the @Deployment method. Show me how!
@Deployment
public static Archive<?> deployment() {
	return ShrinkWrap.create(JavaArchive.class)
               .addClasses(MyBean2.class, MyParameter.class)
			.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
}
Missing a description for the test scenario. Add some javadoc to the @Test method. Show me how!
@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"));
}
Missing a description for the test scenario. Add some javadoc to the @Test method. Show me how!
@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));
}

ConstructorParametersInjectionTest

Missing a description for the story. Add some javadoc to the TestClass. Show me how!
Missing a description for the deployment. Add some javadoc to the @Deployment method. Show me how!
@Deployment
public static Archive<?> deployment() {
	return ShrinkWrap.create(JavaArchive.class)
               .addClasses(MyBean2.class, MyParameter.class)
			.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
}
Missing a description for the test scenario. Add some javadoc to the @Test method. Show me how!
@Test
public void constructorViolationsWhenNullParameters() {
       thrown.expect(ConstraintViolationException.class);
       thrown.expectMessage("javax.validation.constraints.NotNull");
       thrown.expectMessage("MyBean2.arg0.value");
	bean.getValue();
}

MethodParametersConstraintsTest

Missing a description for the story. Add some javadoc to the TestClass. Show me how!
Missing a description for the deployment. Add some javadoc to the @Deployment method. Show me how!
@Deployment
public static Archive<?> deployment() {
	return ShrinkWrap.create(JavaArchive.class).addClasses(MyBean.class)
			.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
}
Missing a description for the test scenario. Add some javadoc to the @Test method. Show me how!
@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");
}
Missing a description for the test scenario. Add some javadoc to the @Test method. Show me how!
@Test
public void methodSizeOk() {
	bean.sayHello("Duk");
}
Missing a description for the test scenario. Add some javadoc to the @Test method. Show me how!
@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);
}
Missing a description for the test scenario. Add some javadoc to the @Test method. Show me how!
@Test
public void showDateFromFuture() {
	bean.showDate(true);
}
Missing a description for the test scenario. Add some javadoc to the @Test method. Show me how!
@Test
public void multipleParametersWithEmptyList() {
	thrown.expect(ConstraintViolationException.class);
	thrown.expectMessage("javax.validation.constraints.Size");
	thrown.expectMessage("showList.arg0");
	bean.showList(new ArrayList<String>(), "foo");
}
Missing a description for the test scenario. Add some javadoc to the @Test method. Show me how!
@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);
}
Missing a description for the test scenario. Add some javadoc to the @Test method. Show me how!
@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 ")));
}

Share the Knowledge

Find this sample useful? Share on

There's a lot more about JavaEE to cover. If you're ready to learn more, check out the other available samples.

Help Improve

Find a bug in the sample? Something missing? You can fix it by editing the source, making the correction and sending a pull request. Or report the problem to the issue tracker

Recent Changelog

  • Jul 15, 2014: Removed header license. the licensing is now referenced in the license file in the root of the project by Roberto Cortez
  • Nov 26, 2013: Typo by kubamarchwicki
  • Nov 26, 2013: Constructor validation injection by kubamarchwicki
  • Nov 22, 2013: Fixing compilation errors by Arun Gupta
  • Nov 14, 2013: Constructor validation check with instance injection by kubamarchwicki
  • Nov 14, 2013: Constructor validation tests for arun-gupta/javaee7-samples#85 by kubamarchwicki
  • Nov 13, 2013: Method validation tests for arun-gupta/javaee7-samples#85 by kubamarchwicki
  • Sep 17, 2013: Removing netbeans configuration file by Arun Gupta
  • Sep 12, 2013: Using cdi 1.1 "all" style beans.xml by Arun Gupta
  • Aug 28, 2013: Slightly more intuitive titles by Arun Gupta
How to help improve this sample
The source code for this sample can be found in the javaee7-samples GitHub repository. The first thing you need to do is to get the source by downloading the repository and then go into the samples folder:
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!