How to autowire service in ConstraintValidator

前端 未结 3 1902

I\'m writting my application using Spring MVC. I want to validate is e-mail exists in database when user is registering. I\'ve written my own annotation constraint named

3条回答
  •  遇见更好的自我
    2021-01-19 16:41

    If your main code is working, then it should be straight forward to get your test working. You need to use @ContextConfiguration on your test class, see this for more details: http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/testing.html

    In general, there are 2 ways to test this:

    • unit test
    • integration test

    For unit test, you need to create an instance of the UniqueEmailValidator and set a UserService on that instance (normally a mock UserServer).

    For integration test, you need to have the spring context initialized as I mentioned above.

提交回复
热议问题