1. @Data
    2. @Accessors(chain = true)
    3. public class TypeEntity {
    4. /**
    5. * 没有必要设置type
    6. */
    7. @Matcher(type = Integer.class)
    8. private Integer data;
    9. @Matcher(type = CharSequence.class)
    10. private String name;
    11. @Matcher(type = {Integer.class, Float.class})
    12. private Object obj;
    13. @Matcher(type = Number.class)
    14. private Object num;
    15. }

    测试代码

    1. def "测试不明写类继承关系1"() {
    2. given:
    3. TypeEntity entity = new TypeEntity().setObj(obj)
    4. expect:
    5. boolean actResult = MkValidators.check(entity, "obj")
    6. if (!result) {
    7. println MkValidators.getErrMsgChain()
    8. }
    9. Assert.assertEquals(result, actResult)
    10. where:
    11. obj | result
    12. 'c' | false
    13. "abad" | false
    14. 1232 | true
    15. 1232l | false
    16. 1232f | true
    17. 12.0f | true
    18. -12 | true
    19. }

    注意:

    1. 如果设置的类型不是属性的类型或者父类则会报错
    2. 如果为具体的类型,则再设置与其相同的类型,则没有必要,就像上面的data属性