title: Spring - Validator does not trigger in integration test
body: <h2>The problem</h2>
  <p>Actually, this is a kind of <a href="https://stackoverflow.com/questions/40870242/spring-integration-test-of-multipartfileupload-with-container-and-custom-valid">&quot;follow-up&quot; question</a>. I have 2 integration tests running for a file upload, testing the corresponding controller. Unfortunately, after using the solution of the answer from my other question, the second integration test that should &quot;fail&quot; (because the extension of the file is &quot;wrong&quot; and therefore the FileContainerValidator should reject the file) does not fail! The Validator doesn't even get called.</p>
  <p>How to solve the problem?</p>
  <hr />
  <h2>My code</h2>
  <p>FileUploadController</p>
  <pre><code>@Controller
  public class FileUploadController {
  
      private final FileStorageService fileStorageService;
  
      FileContainerValidator fileContainerValidator;
  
      @Autowired
      public FileUploadController(FileStorageService FileStorageService) {
          this.fileStorageService = FileStorageService;
      }
  
      @Autowired
      public void setDefaultFileContainerValidator(FileContainerValidator validator) {
          this.fileContainerValidator = validator;
      }
  
      @GetMapping(&quot;/upload&quot;)
      public String showTestFileUploadForm(@ModelAttribute Mapping mapping, FileContainer fileContainer, Model model) {
          String path = &quot;ERROR&quot;;
          try {
              path = new ClassPathResource(&quot;data.csv&quot;).getFile().getPath();
          } catch (IOException e) {
              e.printStackTrace();
          }
  
          model.addAttribute(&quot;shortenedFile&quot;, new TableConstructor(path, mapping.getContentDelimiter()));
          return &quot;upload&quot;;
      }
  
      @PostMapping(&quot;/upload&quot;)
      public String uploadFile(Model model, @Valid FileContainer fileContainer, BindingResult result) {
          if (!result.hasErrors()) {
              System.out.println(&quot;Fetching file&quot;);
              fileStorageService.store(fileContainer);
              //TODO: SUCCESS
              model.addAttribute(&quot;success&quot;, true);
          }
  
          return &quot;upload&quot;;
      }
  
      @InitBinder(&quot;fileContainer&quot;)
      protected void initBinderFileContainer(WebDataBinder binder) {
          binder.setValidator(fileContainerValidator);
      }
  }
  </code></pre>
  <p>FileUploadIntegrationTest</p>
  <pre><code>@RunWith(SpringRunner.class)
  @WebMvcTest(FileUploadController.class)
  public class FileUploadIntegrationTest {
  
      @Autowired
      private MockMvc mockMvc;
  
      @MockBean
      private FileStorageService fileStorageService;
  
      @MockBean
      private FileContainerValidator fileContainerValidator;
  
      private MockMultipartFile correctFile =
              new MockMultipartFile(&quot;file&quot;, &quot;filename.xml&quot;, &quot;text/plain&quot;, &quot;some xml&quot;.getBytes());
  
      private MockMultipartFile fileWithWrongExtension =
              new MockMultipartFile(&quot;file&quot;, &quot;filename.txt&quot;, &quot;text/plain&quot;, &quot;this is a wrong file!&quot;.getBytes());
  
      private String testDestination = &quot;/tmp&quot;;
  
      //THIS ONE WORKS
      @Test
      public void uploadMultipartTestFile() throws Exception {
          when(fileContainerValidator.supports(any(Class.class))).thenReturn(true);
          mockMvc.perform(MockMvcRequestBuilders.fileUpload(&quot;/upload&quot;)
                  .file(correctFile)
                  .param(&quot;destination&quot;, &quot;/tmp&quot;))
                  .andExpect(model().attribute(&quot;success&quot;, true));
      }
  
      //THIS ONE DOES NOT WORK
      @Test
      public void errorOnUploadFileWithWrongExtension() throws Exception {
        when(fileContainerValidator.supports(any(Class.class))).thenReturn(true);
          mockMvc.perform(MockMvcRequestBuilders.fileUpload(&quot;/upload&quot;)
                  .file(fileWithWrongExtension)
                  .param(&quot;destination&quot;, &quot;/tmp&quot;))
                  .andExpect(model().attributeHasFieldErrors(&quot;fileContainer.file&quot;));
      }
  }
  </code></pre>
  <p>FileContainerValidator</p>
  <pre><code>@Component
  public class FileContainerValidator implements Validator {
      public boolean supports(Class&lt;?&gt; clazz) {
          return FileContainer.class.equals(clazz);
      }
  
      @Override
      public void validate(Object obj, Errors errors) {
          FileContainer fileContainer = (FileContainer) obj;
          MultipartFile file = fileContainer.getFile();
          String destination = fileContainer.getDestination();
          if (file != null) {
              if (file.getSize() == 0) {
                  errors.rejectValue(&quot;file&quot;, &quot;missing.file&quot;, &quot;The file must not be null or empty!&quot;);
              }
              if(file.getOriginalFilename().contains(&quot;.&quot;)) {
                  String extension = file.getOriginalFilename().split(&quot;\\.&quot;)[1];
                  if (!extension.equalsIgnoreCase(&quot;xml&quot;) &amp;&amp; !extension.equalsIgnoreCase(&quot;csv&quot;)) {
                      errors.rejectValue(&quot;file&quot;, &quot;extension.file&quot;, &quot;The file had the wrong extension!&quot;);
                  }
              } else {
                  errors.rejectValue(&quot;file&quot;, &quot;extension.file&quot;, &quot;The file had the wrong extension!&quot;);
              }
          } else {
              errors.rejectValue(&quot;file&quot;, &quot;missing.file&quot;, &quot;The file must not be null or empty!&quot;);
          }
          if(destination != null &amp;&amp; !destination.isEmpty()){
              if(!destination.matches(&quot;([a-zA-Z]:)?(\\/[a-zA-Z0-9_.-]+)+\\/?&quot;))
                  errors.rejectValue(&quot;destination&quot;, &quot;wrong.destination&quot;, &quot;The destination is given wrong&quot;);
          } else {
              errors.rejectValue(&quot;destination&quot;, &quot;missing.destination&quot;, &quot;The destination is missing&quot;);
          }
      }
  }
  </code></pre>
  <p>Form (&quot;upload.html&quot;)</p>
  <pre><code>&lt;!DOCTYPE html SYSTEM &quot;http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd&quot;&gt;
  &lt;html xmlns:th=&quot;http://www.thymeleaf.org&quot; lang=&quot;en&quot; xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
  &lt;head th:include=&quot;head :: head (pagename='Upload File')&quot;&gt;&lt;/head&gt;
  
  &lt;body&gt;
  
  &lt;div th:replace=&quot;fragments/nav :: nav&quot;&gt;&amp;copy; Static&lt;/div&gt;
  &lt;div class=&quot;panel panel-default&quot; id=&quot;welcome-panel&quot;&gt;
      &lt;div class=&quot;panel-heading&quot;&gt;
          &lt;h2&gt;Upload File&lt;/h2&gt;
      &lt;/div&gt;
      &lt;div class=&quot;panel-body&quot;&gt;
          &lt;div class=&quot;alert alert-success&quot; th:if=&quot;${success}&quot;&gt;File successfully uploaded!&lt;/div&gt;
          &lt;form method=&quot;POST&quot; action=&quot;#&quot; th:action=&quot;@{/upload}&quot; th:object=&quot;${fileContainer}&quot;
                enctype=&quot;multipart/form-data&quot;&gt;
              &lt;h4&gt;Upload files here:&lt;/h4&gt;
              &lt;label for=&quot;file&quot; class=&quot;upload-drop-zone&quot;&gt;
                  Just drag and drop files &lt;br&gt; or click to upload.
                  &lt;input type=&quot;file&quot; th:field=&quot;*{file}&quot; id=&quot;file&quot; style=&quot;display:none;&quot;/&gt;
              &lt;/label&gt;
              &lt;br&gt;&lt;br&gt;
              &lt;label for=&quot;destination&quot;&gt;Destination:&lt;/label&gt;
              &lt;input type=&quot;text&quot; value=&quot;/tmp&quot; id=&quot;destination&quot; th:field=&quot;*{destination}&quot;/&gt;
              &lt;br&gt;
              &lt;button class=&quot;btn btn-success convert-button btn-block&quot; type=&quot;submit&quot; name=&quot;upload&quot; id=&quot;upload&quot;&gt;Create
              &lt;/button&gt;
              &lt;div class=&quot;alert alert-danger&quot; th:if=&quot;${#fields.hasErrors('file')}&quot; th:errors=&quot;*{file}&quot;&gt;&lt;/div&gt;
              &lt;div class=&quot;alert alert-danger&quot; th:if=&quot;${#fields.hasErrors('destination')}&quot;
                   th:errors=&quot;*{destination}&quot;&gt;&lt;/div&gt;
          &lt;/form&gt;
  
      &lt;/div&gt;
  &lt;/div&gt;
  &lt;/div&gt;
  
  &lt;/body&gt;
  
  &lt;/html&gt;
  </code></pre>