How to test validation annotations of a class using JUnit?

后端 未结 9 1119
盖世英雄少女心
盖世英雄少女心 2020-12-07 17:29

I need to test the validation annotations but it looks like they do not work. I am not sure if the JUnit is also correct. Currently, the test will be passed but as you can s

9条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-07 18:08

    such as:

    public class Test {
        @Autowired
        private Validator validator;
        public void testContactSuccess() {
            Contact contact = new Contact();
            contact.setEmail("Jackyahoo.com");
            contact.setName("Jack");
            System.err.println(contact);
            Set> violations = validator.validate(contact);
            assertTrue(violations.isEmpty());
        }
    }
    

    and you also need add bean autowired in your context.xml, such as:

    
    
    

提交回复
热议问题