4

i have controllers with @Valid annotation. by default, mockMvc tests triggers that validation. i would like to disable this for some tests because it requires much more work to prepare valid input, while only partial input is enough for most tests

piotrek
  • 13,982
  • 13
  • 79
  • 165

1 Answers1

4

If you are using a standalone setup, you can provide a mock Validator instance through setValidator method:

mockMvc = MockMvcBuilders.standaloneSetup(controller).setValidator(mockValidator);

If not, you would have to provide a mock Validator bean via a test @Configuration and mark the bean as @Primary. Preferably, you would have a different profile for those kinds of tests so as not to disable validation for other ones.

Miloš Milivojević
  • 5,219
  • 3
  • 26
  • 39