单元测试形式豆良好的模式,具有基于注解的验证Spring MVC中 [英] Good patterns for unit testing form beans that have annotation-based validation in Spring MVC

查看:127
本文介绍了单元测试形式豆良好的模式,具有基于注解的验证Spring MVC中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用基于注解验证了表单bean,什么是最好的做法单元测试,以确保注释为每个字段指定了正确验证这些bean?

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?

例如,如果您有:

public class MyForm {
    @NotNull
    private String name;
}

什么是验证 @NotNull 适用于它的最佳方式?

一个显而易见的方法是创建一个验证器,它抛出一个空,并希望它失败。但在我看来,这是不是最好的方式,你会使用的是测试 @NotNull 的行为和执行,而不是信任的框架。

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.

理想我想使用反射或实用工具,给我的只是声称一个 @NotNull (和任何其他)验证适用于某一领域的可能性,而不是发送验证失败的值的各种组合。

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?

推荐答案

您也可以编写单元测试你的bean这些注释JSR303使用验证器厂全光照。
见例如:
<一href=\"http://musingsofaprogrammingaddict.blogspot.com/2009/02/using-bean-validation-with-spring.html\" rel=\"nofollow\">http://musingsofaprogrammingaddict.blogspot.com/2009/02/using-bean-validation-with-spring.html

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

这篇关于单元测试形式豆良好的模式,具有基于注解的验证Spring MVC中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆