13

When using annotation based validation for a form bean, what is the best practice for unit-testing those beans in order to ensure that correct validations annotations are specified for each field?

For example, if you have:

public class MyForm {
    @NotNull
    private String name;
}

What is the best way to verify that @NotNull is applied to it?

One obvious way is to create a validator, throw a null at it and expect it to fail. But in my view this is not the best way as you'll be testing the behaviour and implementation of @NotNull using that rather than trusting the framework.

Ideally I would want to use reflection or a utility that gives me the possibility of just asserting that an @NotNull (and any other) validation is applied to a given field, rather than having to send various combination of values that fail validation.

Is there an elegant way of doing this, or am I on the right track in general?

mkobit
  • 43,979
  • 12
  • 156
  • 150
Ashkan Aryan
  • 3,504
  • 4
  • 30
  • 44

4 Answers4

6

Two things you should consider:

Do Not test your third party libraries/frameworks.

You should rely on them, they are supposed to be already tested by their maintainers and the surrounding community usage. You don't test them, you rather assess them. To make sure that they fit your needs and mitigate the risks.

Testing behavior is what matter, really!

In the vast majority of applications there is little place for real UNIT testing as the business logic is either small are widespread across multiple modules of the application. So you should consider Integration testing with the same priority than Unit testing. And this is more easily captured by using a behavioral approach.

So, to answer your question, you should not try to Unit test a form bean at all. It's just a transport object. What you should test is how the receiver react with that form, and check both the normal case and the edge cases.

gizmo
  • 11,819
  • 6
  • 44
  • 61
  • 2
    I agree with not testing third party libraries, however testing your configuration is as important as testing behaviour. – František Hartman Mar 01 '12 at 13:10
  • Yes, that is a good point and I agree with that. However I think (as frant.hartm says) that there's room to test the configuration. Everything else (i.e. actual validation) has nothing to do with the bean directly and should not be tested here. – Ashkan Aryan Mar 01 '12 at 13:18
  • 1
    +1 for 'So you should consider Integration testing with the same priority than Unit testing.' – artbristol Mar 02 '12 at 10:57
4

You can also write unit tests for your Beans which are annotated usin JSR303 using validator factory. See example: http://musingsofaprogrammingaddict.blogspot.com/2009/02/using-bean-validation-with-spring.html

Erhan Bagdemir
  • 5,231
  • 6
  • 34
  • 40
  • Thank you. The main article in the post is actually doing it the way I want to avoid. However there's a comment on that post (from gunnar) that's doing it the way I was hoping - checking the annotation rather than the behaviour of the annotation. – Ashkan Aryan Mar 01 '12 at 13:17
  • 2
    Please add mandantory parts of the linked article as part of your answer to prevent loosing information if the page goes offline. – CSchulz Jan 26 '16 at 10:41
4

You can test it easily.

Let's say you are using Hibernate Validator. More or less, it should be something like this

    import javax.validation.ConstraintViolation;
    import junit.framework.Assert;
    import org.hibernate.validator.HibernateValidator;
    import org.junit.Before;
    import org.junit.Test;
    import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;

 private LocalValidatorFactoryBean localValidatorFactory;


@Before
public void setup() {
    localValidatorFactory = new LocalValidatorFactoryBean();
    localValidatorFactory.setProviderClass(HibernateValidator.class);
    localValidatorFactory.afterPropertiesSet();
}

  @Test
  public void testNullValidationError() {
        final MyForm myForm= new MyForm ();
        myForm.setName(null);
        Set<ConstraintViolation<MyForm >> constraintViolations =      localValidatorFactory.validate(myForm);
        Assert.assertTrue("Your error message", constraintViolations.notNull == null);
    }
danny.lesnik
  • 18,479
  • 29
  • 135
  • 200
  • No, this is what I exactly want to avoid in the unit test for the bean, as explained in the original post and follow ups. – Ashkan Aryan Mar 01 '12 at 14:39
2

We test the annotations at bean properties as part of integration testing between our presentation layer and spring container.

What we do is create fake MockPortletContext, DispatcherPortlet and MockRequests (these classes are part of spring-test library), fill the requests so they look like real form was submited and then call the dispatcherPortlet. (we have portlet environment but it doesnt matter)

Then you can check that your backend was called approprietly or that the response contains binding result with expected validation errors which is what you need..

František Hartman
  • 14,436
  • 2
  • 40
  • 60