title: Spring MVC test failure
body: <p>I'm writing simple integration tests for my app using the Spring MVC Test framework. I have two basic test cases:</p>
  
  <ol>
  <li>The Add link form is filled in correctly (the URL and optional description input fields are entered) and a link is added to a database via POST and then the client is redirected to the /link URL.</li>
  <li>The Add link form is empty, so the /links/create view is rendered and form errors from BindingResult are presented.</li>
  </ol>
  
  <p>The testAddLink() test passes successfully, but the problem occurs with the testAddEmptyLink() test method.
  In this test, the 'links/create' view should be rendered and I should get a 200 status code once the expression</p>
  
  <pre><code>result.hasErrors()
  </code></pre>
  
  <p>is true.</p>
  
  <p>However, I'm getting a 302 response, which should be sent when there are no errors in the form (the URL has been set correctly)
  It seems the test method testAddEmptyLink() cannot deal with form errors with BindingResult.</p>
  
  <p>Do you have any ideas what could be the cause it cannot deal with form errors?</p>
  
  <p>Thanks in advance.</p>
  
  <p>Link Entity</p>
  
  <pre><code>@Entity
  @Table(name = "links")
  public class Link {
  
  @Id @GeneratedValue
  private Integer ID;
  
  @Column(name = "url") @NotNull @NotEmpty @URL
  private String URL;
  
  private String description;
  
  @Column(name="created_at", nullable = false)
  @Type(type="org.jadira.usertype.dateandtime.joda.PersistentLocalDateTime")
  private LocalDateTime createdAt;
  
  @Column(name="updated_at", nullable = true)
  @Type(type="org.jadira.usertype.dateandtime.joda.PersistentLocalDateTime")
  private LocalDateTime updatedAt;
  
  @ManyToOne
  @JoinColumn(name = "category_id")
  private Category category;
  
  public Integer getID() {
      return ID;
  }
  
  public void setID(Integer ID) {
      this.ID = ID;
  }
  
  public String getURL() {
      return URL;
  }
  
  public void setURL(String URL) {
      this.URL = URL;
  }
  
  public String getDescription() {
      return description;
  }
  
  public void setDescription(String description) {
      this.description = description;
  }
  
  public LocalDateTime getCreatedAt() {
      return createdAt;
  }
  
  public void setCreatedAt(LocalDateTime createdAt) {
      this.createdAt = createdAt;
  }
  
  public LocalDateTime getUpdateddAt() {
      return updatedAt;
  }
  
  public void setUpdateddAt(LocalDateTime updateddAt) {
      this.updatedAt = updateddAt;
  }
  
  public LocalDateTime getUpdatedAt() {
      return updatedAt;
  }
  
  public void setUpdatedAt(LocalDateTime updatedAt) {
      this.updatedAt = updatedAt;
  }
  
  public Category getCategory() {
      return category;
  }
  
  public void setCategory(Category category) {
      this.category = category;
  }
  
  }
  </code></pre>
  
  <p>LinkController</p>
  
  <pre><code>@Controller
  public class LinkController {
  
  @Autowired(required = true) @Qualifier(value = "linkService")
  private LinkService linkService;
  
  @Autowired
  private CategoryService categoryService;
  
  @RequestMapping(value = "/links")
  public String getLinks(Model model) {
      model.addAttribute("results", linkService.getLinks());
  
      return "links/index";
  }
  
  @RequestMapping(value = "/links/create", method = RequestMethod.GET)
  public ModelAndView showLinkForm() {
      ModelAndView model = new ModelAndView("links/create");
      model.addObject("link", new Link());
      model.addObject("categories", categoryService.getCategories());
      return model;
  }
  
  @RequestMapping(value = "/links/create", method = RequestMethod.POST)
  public String addLink(@Valid @ModelAttribute("link") Link link, BindingResult result, Model model) {
      if (result.hasErrors()) {
          model.addAttribute("categories", categoryService.getCategories());
          return "links/create";
      }
  
      linkService.addLink(link);
  
      return "redirect:/links";
  }
  }
  </code></pre>
  
  <p><strong>LinkControllerTest</strong></p>
  
  <pre><code>@ContextConfiguration(classes = {AppInitializer.class})
  @RunWith(SpringJUnit4ClassRunner.class)
  public class LinkControllerTest {
  
  @Mock
  private LinkService linkService;
  
  @InjectMocks
  private LinkController linkController;
  
  private MockMvc mockMvc;
  
  @Before
  public void setUp() {
      // Process mock annotations
      MockitoAnnotations.initMocks(this);
  
      this.mockMvc = MockMvcBuilders.standaloneSetup(linkController).build();
  }
  
  @Test
  public void testAddLink() throws Exception {
      mockMvc.perform(post("/links/create")
          .param("URL", "http://test.com")
          .param("description", "Lorem Ipsum")
          .param("category.ID", "1"))
          .andExpect(status().is3xxRedirection())
          .andExpect(view().name("redirect:/links"));
  }
  
  @Test
  public void testAddEmptyLink() throws Exception {
      mockMvc.perform(post("/links/create")
              .contentType(MediaType.APPLICATION_FORM_URLENCODED)
              .sessionAttr("link", new Link())
      )
              .andExpect(status().isOk())
              .andExpect(view().name("links/create"))
              .andExpect(forwardedUrl("/WEB-INF/views/links/create.jsp"))
              .andExpect(model().attributeHasFieldErrors("link", "URL", "description"))
              .andExpect(model().attribute("link", hasProperty("URL", isEmptyOrNullString())))
              .andExpect(model().attribute("link", hasProperty("description", isEmptyOrNullString())));
  }
  }
  </code></pre>
  
  <p>create.jsp (View)</p>
  
  <pre><code>&lt;%@page contentType="text/html" pageEncoding="UTF-8"%&gt;
  &lt;%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %&gt;
  &lt;%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%&gt;
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;meta http-equiv="Content-Type" content="text/html; charset=windows-1251"&gt;
          &lt;title&gt;Create Category&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;form:form action="" modelAttribute="category"&gt;
              &lt;div&gt;
                  &lt;form:label path="name"&gt;Name&lt;/form:label&gt;
                  &lt;form:input path="name" /&gt;
                  &lt;form:errors path="name" cssClass="error"&gt;&lt;/form:errors&gt;
              &lt;/div&gt;
              &lt;div&gt;
                  &lt;form:label path="description"&gt;Description&lt;/form:label&gt;
                  &lt;form:input path="description" /&gt;
                  &lt;form:errors path="description" cssClass="error"&gt;&lt;/form:errors&gt;
              &lt;/div&gt;
              &lt;div&gt;
                  &lt;input type="submit" value="Create Category"&gt;
                  &lt;input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/&gt;
              &lt;/div&gt;
          &lt;/form:form&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>