@Test public void testPostAccountSuccess() throws Exception { Account a = accountManager.findAccount("123456001"); a.setEmail("*****@*****.**"); // Update the email mockMvc .perform( post("/accounts/{acctId}", "123456001") .param("name", a.getName()) // Providing all the params that would have been sent in // the form .param("dateOfBirth", formatDate(a.getDateOfBirth())) .param("email", a.getEmail()) .param("receiveNewsletter", (a.isReceiveNewsletter() ? "1" : "0")) .param("receiveMonthlyEmailUpdate", (a.isReceiveMonthlyEmailUpdate() ? "1" : "0"))) .andExpect(status().isFound()) // Because this is a redirect .andExpect(redirectedUrl("123456001")); // The redirect URL // provided by the // controller }
@Test public void testPostAccountFailValidation() throws Exception { Account a = accountManager.findAccount("123456001"); a.setEmail("bogusemail"); // Update the email with bad email address mockMvc .perform( post("/accounts/{acctId}", "123456001") .param("name", a.getName()) // Providing all the params that would have been sent in // the form .param("dateOfBirth", formatDate(a.getDateOfBirth())) .param("email", a.getEmail()) .param("receiveNewsletter", (a.isReceiveNewsletter() ? "1" : "0")) .param("receiveMonthlyEmailUpdate", (a.isReceiveMonthlyEmailUpdate() ? "1" : "0"))) .andExpect(model().attributeHasErrors("account")) .andExpect(model().attributeErrorCount("account", 1)) .andExpect(model().attributeHasFieldErrors("account", "email")) .andExpect(status().isOk()) // Because this is a forward to the // same view .andExpect(view().name("accounts/edit")); }