Good patterns for unit testing form beans that have annotation-based validation in Spring MVC

前端 未结 4 1371
旧时难觅i
旧时难觅i 2020-12-20 13:06

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 sp

相关标签:
4条回答
  • 2020-12-20 13:47

    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.

    0 讨论(0)
  • 2020-12-20 13:50

    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..

    0 讨论(0)
  • 2020-12-20 13:56

    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

    0 讨论(0)
  • 2020-12-20 14:09

    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);
        }
    
    0 讨论(0)
提交回复
热议问题