gpt4 book ai didi

java - 当 beanType 是接口(interface)时 Validator.validateValue 忽略验证(hibernate-validator-6.0.18)

转载 作者:行者123 更新时间:2023-12-01 18:08:23 26 4
gpt4 key购买 nike

我创建了一个库,其中仅包含对域对象建模的接口(interface)。这些接口(interface)具有我想要测试的(类、属性、方法和构造函数级别)约束。

例如:

public interface User {
@NotNull
@Email
String getEmail();
}

我想测试约束是否得到正确应用,所以我编写了一些测试。然而,我得到了意想不到的行为。

class UserTest {
@Test
void thisTestFails() {
Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
Set<ConstraintViolation<User>> violations = validator.validateValue(User.class, "email", null);
assertEquals(1, violations.size());
}

@Test
void thisTestPasses() {
Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
Set<ConstraintViolation<UserImpl>> violations = validator.validateValue(UserImpl.class, "email", null);
assertEquals(1, violations.size());
}

private static class UserImpl implements User {
@Override
public String getEmail() {
return null;
}
}
}

当为 Class<T> beanType 提供参数时,Hibernate 完全忽略验证是一个接口(interface)。我不想创建具体的类只是为了测试这些接口(interface)。这是 Hibernate 中的预期行为还是错误?

最佳答案

这是预期的行为。 Bean 验证规范定义可以在接口(interface)或类中定义约束,但验证的是遵循 Java Beans 约定的属性状态。

接口(interface)可以定义由类实现的 getter 方法,但它本身没有属性。

这是规范的相关部分:

5.1.1. Object validation

Constraint declarations can be applied to a class or an interface. Applying a constraint to a class or interfaceexpresses a validation over the state of the class or the classimplementing the interface.

5.1.2. Field and property validation

Constraint declarations can be applied on both fields and propertiesfor the same object type. The same constraint should however not beduplicated between a field and its associated property (the constraintvalidation would be applied twice). It is recommended for objectsholding constraint declarations to adhere to a single state accessstrategy (either annotated fields or properties).

When a field is annotated with a constraint declaration, field accessstrategy is used to access the state validated by such constraint.

When a property is annotated with a constraint declaration, propertyaccess strategy is used to access the state validated by suchconstraint.

确实,validateValue 方法不需要实例,因此理论上它可以测试接口(interface)的约束。但请注意 class 参数,它是一个 beanType,并且 bean 的类型需要是一个类,因为接口(interface)没有构造函数。

请注意与 Validator 中的方法 getConstraintsForClass 的区别,该方法明确声明可以获得接口(interface)上声明的约束:

class or interface type evaluated

validateValue的类参数:

the bean type

关于java - 当 beanType 是接口(interface)时 Validator.validateValue 忽略验证(hibernate-validator-6.0.18),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60514567/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com