如何验证 JUnit 中静态方法的参数?

How to validate parameters of a static method in JUnit?

我有一个带参数和约束的静态方法。我想 运行 进行一些单元测试,但是当我调用 executableValidator.validateParameters 时,没有出现违规问题(应该是)

我 运行 一些反射代码,我在日志中正确显示了验证注释。

我的 class 使用静态方法:

public class DiceRoller {

    private DiceRoller(){}
    public static int roll(@Max(20) @Valid Integer number, @DiceConstraint @Valid Integer sides){
        return (int) Math.floor(Math.random() * ((sides * number)-number) + number);
    }

}

约束接口:

@Documented
@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = DiceValidator.class)
public @interface DiceConstraint {

    String message() default "You have to choose between dice with 2,4,6,8,10,12,20 or 100 sides";
    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};

}

验证者class:

public class DiceValidator implements ConstraintValidator<DiceConstraint, Integer> {

    protected int validSides [] = {2,4,6,8,10,12,20,100};

    @Override
    public boolean isValid(Integer sides, ConstraintValidatorContext constraintValidatorContext) {
        return Arrays.stream(validSides).anyMatch(i -> i == sides.intValue());

    }
}

和我的测试:

@SpringBootTest(classes = {SpringBootCharacterApplication.class})
public class DiceRollerCoCTest {

    private static ValidatorFactory factory;
    private static Validator validator;
    private static ExecutableValidator executableValidator;

    @BeforeEach
    public void setUp() {
        if (factory == null) {
            factory = Validation.buildDefaultValidatorFactory();
            validator = factory.getValidator();
            executableValidator = validator.forExecutables();
        }
    }

    @Test
    void roll() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        log.debug("Test of DiceRoller.roll(1000, 1000)");
        Method roll = DiceRoller.class.getDeclaredMethod("roll", Integer.class,Integer.class);
        Annotation[][] parameterAnnotations = roll.getParameterAnnotations();
        Arrays.stream(parameterAnnotations)
                .forEach(v->
                        Arrays.stream(v)
                                .forEach(vv -> log.debug(vv.toString())) );
        log.debug("Invocation " + roll.invoke(null, 1000, 1000));
        Object [] parameterValues = {1000, 1000};
        Set<ConstraintViolation<Object>> violations
                = executableValidator.validateParameters(
                        DiceRoller.class,
                        roll,
                        parameterValues);
        log.debug(violations.size());

    }
}

最后一部分:

Object [] parameterValues = {1000, 1000};
        Set<ConstraintViolation<Object>> violations
                = executableValidator.validateParameters(
                        DiceRoller.class,
                        roll,
                        parameterValues);
        log.debug(violations.size());

违规大小应等于 2 但它等于 0。

我还有一个 RestController,它使用顶部带有 @Validated 注释的方法,在这种情况下它可以正常工作。所以当我 运行 这个单元测试时,我正在等待相同的行为。

我的错误是什么? 谢谢

名称"bean validation framework"表明它仅适用于Java Beans,它们始终是对象,静态方法在技术上不属于Bean的概念。

这是本页的第一条语句:https://javaee.github.io/tutorial/bean-validation004.html

"Bean Validation constraints may be placed on the parameters of nonstatic methods and constructors and on the return values of nonstatic methods. Static methods and constructors will not be validated."