@UsernameAlreadyExists
private String username;
I have a custom validator that I created to ensure that duplicate usernames are caught by the application when account creation form submits.
When I unit test the account creation controller using MockMVC, it fails as the validator depends on a service, so I get null pointer exception.
How can I mock the validator or the service this validator depends on? I could not figure out how to make this work as the controller does not depend on the validator explicitly, it runs outside of the controller.
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.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments