如何测试@Valid [英] How to test @Valid

查看:175
本文介绍了如何测试@Valid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的实体中,我有一些用于验证的休眠注释,例如@ NotEmpty,@ Pattern ..等

In my entities I have some hibernate annotations for validation, like @NotEmpty, @Pattern.. and others

在我的控制器中,执行保存操作时,它具有一个@Valid参数.

In my controller, on save action, it has an @Valid parameter.

但是,如果任何实体具有任何必填字段,并且没有注释,我将会遇到问题.

But if any entity has any required field, and there is no annotation I will have problems.

因此,我想测试每个实体,以确保它们具有必要的注释.

So I would like to test each entity, to ensure they have the necessary notes.

类似的东西:

@Test(expect=IllegalArgumentException.class)
public void testAllNull() {
    Person p = new Persson(); // Person name has an @NotEmpty
    validator.validate(p);
}

但是如何验证呢?谁被要求检查@Valid?

But how to validate it? Who is called to check @Valid?

谢谢.

推荐答案

我发现了如何检查:

    @Autowired
    private LocalValidatorFactoryBean validator;

    ...

    validator.validateProperty(object, propertyName)

这篇关于如何测试@Valid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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