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

led
@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.

Biju Kunjummen

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

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

From Dev

Spring MockMvc - Custom Validators are not registered/called in Spring Container

From Dev

Spring MVC custom validators

From Dev

How to mock autowired dependencies in Spring Boot MockMvc Unit tests?

From Dev

Inject mock into Spring MockMvc WebApplicationContext

From Dev

How to send a mock object as JSON in mockmvc

From Dev

How to conditionally invoke spring validators

From Dev

stubbing two methods in a mock is throwing Exception using Mockito and Spring MockMVC

From Dev

(Laravel) How to add errors in custom validators?

From Dev

How to access models outside controllers in Sails

From Dev

How to access models outside controllers in Sails

From Dev

Tests fail with custom jsr validators with @Inject for Spring boot application

From Dev

Spring Security Unit Test - MockMvc perform test with custom user

From Dev

How to mock a real running applicaton?

From Dev

How to mock Spring ConversionService?

From Dev

How can I fix a missing permission for an outside resource in a CRM2011 custom workflow running in the sandbox?

From Dev

How do I add custom validators in JOI 17?

From Dev

Testing custom validators with Minitest

From Dev

CausesValidation not working with custom validators

From Dev

Django Custom Validators Not Working

From Dev

Proptypes custom validators with Flow

From Dev

How to change the same values everywhere - in service , controllers, outside angular?

From Dev

how to locate resources in Symfony2 outside controllers?

From Dev

how to import json string constants from outside module to angularjs controllers?

From Dev

how to use jmockit with spring's mockmvc to test controller

From Dev

How to PUT multipart/form-data using Spring MockMvc?

From Dev

How to encode JSON as request parameter using Spring MockMVC

From Dev

How to format Spring REST Docs in response body with mockMvc

From Dev

How to use the right Spring Security configuration during a MockMvc test

Related Related

  1. 1

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

  2. 2

    Spring MockMvc - Custom Validators are not registered/called in Spring Container

  3. 3

    Spring MVC custom validators

  4. 4

    How to mock autowired dependencies in Spring Boot MockMvc Unit tests?

  5. 5

    Inject mock into Spring MockMvc WebApplicationContext

  6. 6

    How to send a mock object as JSON in mockmvc

  7. 7

    How to conditionally invoke spring validators

  8. 8

    stubbing two methods in a mock is throwing Exception using Mockito and Spring MockMVC

  9. 9

    (Laravel) How to add errors in custom validators?

  10. 10

    How to access models outside controllers in Sails

  11. 11

    How to access models outside controllers in Sails

  12. 12

    Tests fail with custom jsr validators with @Inject for Spring boot application

  13. 13

    Spring Security Unit Test - MockMvc perform test with custom user

  14. 14

    How to mock a real running applicaton?

  15. 15

    How to mock Spring ConversionService?

  16. 16

    How can I fix a missing permission for an outside resource in a CRM2011 custom workflow running in the sandbox?

  17. 17

    How do I add custom validators in JOI 17?

  18. 18

    Testing custom validators with Minitest

  19. 19

    CausesValidation not working with custom validators

  20. 20

    Django Custom Validators Not Working

  21. 21

    Proptypes custom validators with Flow

  22. 22

    How to change the same values everywhere - in service , controllers, outside angular?

  23. 23

    how to locate resources in Symfony2 outside controllers?

  24. 24

    how to import json string constants from outside module to angularjs controllers?

  25. 25

    how to use jmockit with spring's mockmvc to test controller

  26. 26

    How to PUT multipart/form-data using Spring MockMvc?

  27. 27

    How to encode JSON as request parameter using Spring MockMVC

  28. 28

    How to format Spring REST Docs in response body with mockMvc

  29. 29

    How to use the right Spring Security configuration during a MockMvc test

HotTag

Archive