Spring MockMVC - How to mock custom validators running outside of controllers

前端 未结 2 878
暗喜
暗喜 2020-12-20 03:39
@UsernameAlreadyExists
private String username;

I have a custom validator that I created to ensure that duplicate usernames are caught by the appli

2条回答
  •  不思量自难忘°
    2020-12-20 04:25

    You cannot mock the ConstraintValidator but you certainly can mock the service that the validator depends on, using the usual spring ways of mocking beans, for eg.:

    .1. Potentially define a mock instance with the exact same bean name, ensuring that your config with mock gets loaded after the real instance.

    .2. Test with only the test configuration with only the mock bean defined.

提交回复
热议问题