简体   繁体   English

验证器在测试期间没有被调用

[英]Validator is not being called during test

Annotation for my ConstraintValidator implementation is defined as PARAMETER我的 ConstraintValidator 实现的注释定义为PARAMETER

@Target({PARAMETER, ANNOTATION_TYPE, TYPE_USE})

During the test Validator is not being called at all, but if I change annotation target to FIELD then Validator is starting to get called.在测试期间,根本没有调用 Validator,但是如果我将注释目标更改为FIELD ,那么 Validator 就会开始被调用。 How should approach testing annotation with target set as PARAMETER ?应该如何使用目标集作为PARAMETER来测试注释?

This is my test case这是我的测试用例

public class ExecutionRequestValidatorTest_Local extends BaseValidatorTest {
    private static Validator validator;

    @BeforeAll
    public void init() {
        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        validator = factory.getValidator();

        executionRequestInstance = ExecutionRequestInstance.builder().build();
    }


    @Test
    void isValid(){
        final Set<ConstraintViolation<Tester>> validate = validator.validate(new Tester(executionRequestInstance));
    }

    private class Tester {
        private final ExecutionRequestInstance requestInstance;

        public Tester(ExecutionRequestInstance requestInstance){
            this.requestInstance = requestInstance;
            test(requestInstance);
        }
        public void test(@ValidateExecutionRequest ExecutionRequestInstance request){

        }
    }
}

I made the tests work by adding annotation to class variable我通过向 class 变量添加注释来完成测试

private class Tester {
        @ValidateExecutionRequest <----- NEW
        private final ExecutionRequestInstance requestInstance;

        public Tester(ExecutionRequestInstance requestInstance){
            this.requestInstance = requestInstance;
            test(requestInstance);
        }
        public void test(@ValidateExecutionRequest ExecutionRequestInstance request){

        }
    }

But why does this work?但为什么这行得通? With parameter annotation as in test() method the real application works, although not being called via validate() method, but just by framework calling isValid().使用 test() 方法中的参数注释,真正的应用程序可以工作,尽管不是通过 validate() 方法调用,而是通过框架调用 isValid()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM