开发者

hibernate validator 4.2.0 Final @CreditCardNumber

@Autowired
private Valida开发者_如何学JAVAtor validator;

    @Test
        public void testValidateMethodOfPaymentBadCreditCard() {
            final MethodOfPayment mop = new MethodOfPayment();
            command.setDescription("1234567890");
            final Set<ConstraintViolation<MethodOfPayment>> constraintViolations = this.validator.validateProperty(mop, "cardNumber", Default.class);
            Assert.assertFalse(constraintViolations.isEmpty());
            for (final ConstraintViolation<MethodOfPayment> cv : constraintViolations) {
                Assert.assertEquals(cv.getMessage(), "{error.invalid.cardNumber}");
            }
        }

The unit class has instance of define in my xml.

<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>

my methodofpayment is annotated with

@CreditCardNumber(message="{error.invalid.cardNumber}")
    public String getCardNumber() {
        return this.cardNumber;
    }

This test fails on line saying the constraint violation is empty. I thought it should not be empty right?

What is wrong? What kind of data fails for credit card validation?


null is considered as valid value for most constraints defined in Bean Validation and Hibernate Validator.

Hence you won't get a constraint violation if you don't assign a value to the cardNumber property or annotate it with @NotNull (btw. your listing seems a bit strange, where does command come from?)

The validator for @CreditCardNumber uses the Luhn algorithm for checking credit card numbers.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜