title: Spring 4 MVC Validation not working - BindingResult hasErrors is false
body: <p>I am unit testing a Spring controllers post method (using <code>org.springframework.test.web.servlet.MockMvc</code>), and I'm trying to confirm that when there are validation errors in the form it will send the view back to the form by checking the <code>BindingResult.hasErrors</code> method.</p>
  
  <p>Here is my test</p>
  
  <pre><code>  @Test
    public void testFilterChannelProgrammesWhenChannelListAndGenreListAreEmptyAndProgNameIsTooLong() throws Exception {
      String progName = TestUtil.createStringWithLength(301);
  
      mockMvc.perform(post("/api/filter")
          .contentType(MediaType.APPLICATION_FORM_URLENCODED)
          .param("progName", progName)
          .sessionAttr("filter", new ProgrammeSearchDTO())
          )
          .andExpect(status().isOk())
          .andExpect(view().name("api/filter"))
          .andExpect(forwardedUrl("/WEB-INF/jsp/api/filter.jsp"))
          .andExpect(model().attributeHasFieldErrors("filter", "progName"))
          .andExpect(model().attributeHasFieldErrors("filter", "genreIdList"))
          .andExpect(model().attributeHasFieldErrors("filter", "channelIdList"))        
          .andExpect(model().attribute("filter", hasProperty("progName", is(progName))));
  
      verifyZeroInteractions(channelProgrammeServiceMock);
    }
  </code></pre>
  
  <p>Here is the DTO that the session attribute is bound to</p>
  
  <pre><code>import org.hibernate.validator.constraints.Length;
  import org.hibernate.validator.constraints.NotEmpty;
  
  public class ProgrammeSearchDTO {
  
    @NotEmpty
    private String[] channelIdList;
  
    @NotEmpty
    private String[] genreIdList;
  
    private String fromDateTime;
    private String toDateTime;
  
    @Length(max = 200)
    private String progName;
  
    private boolean subtitled;
    private boolean signed;
    private boolean film;
  
    public String[] getChannelIdList() {
      return channelIdList;
    }
  
    public String getFromDateTime() {
      return fromDateTime;
    }
  
    public String[] getGenreIdList() {
      return genreIdList;
    }  
  
    public String getProgName() {
      return progName;
    }
  
    public String getToDateTime() {
      return toDateTime;
    }
  
    public boolean isFilm() {
      return film;
    }
  
    public boolean isSigned() {
      return signed;
    }
  
    public boolean isSubtitled() {
      return subtitled;
    }
  
    public void setChannelIdList(String[] channelIdList) {
      this.channelIdList = channelIdList;
    }
  
    public void setFilm(boolean film) {
      this.film = film;
    }
  
    public void setFromDateTime(String fromDateTime) {
      this.fromDateTime = fromDateTime;
    }
  
    public void setGenreIdList(String[] genreIdList) {
      this.genreIdList = genreIdList;
    }
  
    public void setProgName(String progName) {
      this.progName = progName;
    }
  
    public void setSigned(boolean signed) {
      this.signed = signed;
    }
  
    public void setSubtitled(boolean subtitled) {
      this.subtitled = subtitled;
    }
  
    public void setToDateTime(String toDateTime) {
      this.toDateTime = toDateTime;
    }  
  
  
  }
  </code></pre>
  
  <p>And the controller method</p>
  
  <pre><code>  @RequestMapping(value = "/api/filter", method = RequestMethod.POST)
    public String filterChannelProgrammes(@Valid @ModelAttribute ProgrammeSearchDTO programmeSearchDTO, BindingResult result, Model model) {
      if(result.hasErrors()) {
        return "api/filter";
      }
      model.addAttribute("results", null);
      return "redirect:filterResults";
    }
  </code></pre>
  
  <p>For this test the <code>return "api/filter";</code> should be actioned, but <code>hasErrors()</code> is always false. I have also tried with the following</p>
  
  <pre><code>  @RequestMapping(value = "/api/filter", method = RequestMethod.POST)
    public String filterChannelProgrammes(@Valid @ModelAttribute("filter") ProgrammeSearchDTO programmeSearchDTO, BindingResult result, Model model) {
      if(result.hasErrors()) {
        return "api/filter";
      }
      model.addAttribute("results", null);
      return "redirect:filterResults";
    }
  </code></pre>
  
  <p>But <code>hasErrors()</code> is still false</p>
  
  <p><strong>EDIT</strong> </p>
  
  <p>After some more digging I have this sorted, it also required the following in the context config xml</p>
  
  <pre><code>&lt;mvc:annotation-driven /&gt;
  
  &lt;bean id="validator"
        class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/&gt;
  </code></pre>
  
  <p>And these dependencies in the maven pom.xml</p>
  
  <pre><code>    &lt;dependency&gt;
          &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
          &lt;artifactId&gt;hibernate-validator&lt;/artifactId&gt;
          &lt;version&gt;5.1.2.Final&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;javax.el&lt;/groupId&gt;
          &lt;artifactId&gt;javax.el-api&lt;/artifactId&gt;
          &lt;version&gt;2.2.4&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.glassfish.web&lt;/groupId&gt;
          &lt;artifactId&gt;javax.el&lt;/artifactId&gt;
          &lt;version&gt;2.2.4&lt;/version&gt;
      &lt;/dependency&gt;
  </code></pre>