@Test public void testEqualTo() throws Exception { this.mockMvc.perform(post("/persons")) .andExpect(flash().attribute("one", "1")) .andExpect(flash().attribute("two", 2.222)) .andExpect(flash().attribute("three", new URL("http://example.com"))) .andExpect(flash().attribute("one", equalTo("1"))) // Hamcrest... .andExpect(flash().attribute("two", equalTo(2.222))) .andExpect(flash().attribute("three", equalTo(new URL("http://example.com")))); }
@Test public void testExists() throws Exception { this.mockMvc.perform(post("/persons")) .andExpect(flash().attributeExists("one", "two", "three")); }
@Before public void setup() { this.mockMvc = standaloneSetup(new PersonController()) .alwaysExpect(status().isFound()) .alwaysExpect(flash().attributeCount(3)) .build(); }
@Test public void saveSpecial() throws Exception { this.mockMvc.perform(post("/people").param("name", "Andy")) .andExpect(status().isFound()) .andExpect(redirectedUrl("/persons/Joe")) .andExpect(model().size(1)) .andExpect(model().attributeExists("name")) .andExpect(flash().attributeCount(1)) .andExpect(flash().attribute("message", "success!")); }
@Test public void save() throws Exception { this.mockMvc.perform(post("/persons").param("name", "Andy")) .andExpect(status().isFound()) .andExpect(redirectedUrl("/persons/Joe")) .andExpect(model().size(1)) .andExpect(model().attributeExists("name")) .andExpect(flash().attributeCount(1)) .andExpect(flash().attribute("message", "success!")); }
@Test public void whenFiltersCompleteMvcProcessesRequest() throws Exception { standaloneSetup(new PersonController()) .addFilters(new ContinueFilter()).build() .perform(post("/persons").param("name", "Andy")) .andExpect(status().isFound()) .andExpect(redirectedUrl("/person/1")) .andExpect(model().size(1)) .andExpect(model().attributeExists("id")) .andExpect(flash().attributeCount(1)) .andExpect(flash().attribute("message", "success!")); }
@Test public void filterSkipped() throws Exception { standaloneSetup(new PersonController()) .addFilter(new RedirectFilter(), "/p", "/person").build() .perform(post("/persons").param("name", "Andy")) .andExpect(status().isFound()) .andExpect(redirectedUrl("/person/1")) .andExpect(model().size(1)) .andExpect(model().attributeExists("id")) .andExpect(flash().attributeCount(1)) .andExpect(flash().attribute("message", "success!")); }
@Test public void saveWithErrors() throws Exception { this.mockMvc.perform(post("/persons")) .andExpect(status().isOk()) .andExpect(forwardedUrl("persons/add")) .andExpect(model().size(1)) .andExpect(model().attributeExists("person")) .andExpect(flash().attributeCount(0)); }
@Test public void saveSpecialWithErrors() throws Exception { this.mockMvc.perform(post("/people")) .andExpect(status().isOk()) .andExpect(forwardedUrl("persons/add")) .andExpect(model().size(1)) .andExpect(model().attributeExists("person")) .andExpect(flash().attributeCount(0)); }
@Test public void getPerson() throws Exception { this.mockMvc.perform(get("/persons/Joe").flashAttr("message", "success!")) .andExpect(status().isOk()) .andExpect(forwardedUrl("persons/index")) .andExpect(model().size(2)) .andExpect(model().attribute("person", new Person("Joe"))) .andExpect(model().attribute("message", "success!")) .andExpect(flash().attributeCount(0)); }
protected ResultMatcher redirectAlert(String type, String msg) { return MockMvcResultMatchers.flash().attribute("alerts", new AlertMatcher(type, msg)); }