@Test public void method() throws Exception { Method method = SimpleController.class.getMethod("handle"); this.mockMvc.perform(get("/")).andExpect(handler().method(method)); }
@Test public void testContentAsBytes() throws Exception { this.mockMvc.perform(get("/handle").accept(MediaType.TEXT_PLAIN)) .andExpect(content().bytes("Hello world!".getBytes("ISO-8859-1"))); this.mockMvc.perform(get("/handleUtf8")) .andExpect(content().bytes("\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01".getBytes("UTF-8"))); }
@Test public void testContentStringMatcher() throws Exception { this.mockMvc.perform(get("/handle").accept(MediaType.TEXT_PLAIN)) .andExpect(content().string(containsString("world"))); }
@Test public void requestScopedService() throws Exception { assertTrue("request-scoped service must be a CGLIB proxy", AopUtils.isCglibProxy(this.requestScopedService)); this.mockMvc.perform(get("/requestScopedService").requestAttr(FROM_MVC_TEST_MOCK, FROM_MVC_TEST_MOCK)); }
@Test public void testExists() throws Exception { this.mockMvc.perform(post("/persons")) .andExpect(flash().attributeExists("one", "two", "three")); }
@Test public void testSessionAttributeEqualTo() throws Exception { this.mockMvc.perform(get("/")) .andExpect(request().sessionAttribute("locale", Locale.UK)) .andExpect(request().sessionAttribute("locale", equalTo(Locale.UK))); }
@Test public void testEqualTo() throws Exception { this.mockMvc.perform(get("/")) .andExpect(view().name("mySpecialView")) .andExpect(view().name(equalTo("mySpecialView"))); }
@Test public void testAttributeHamcrestMatchers() throws Exception { mockMvc.perform(get("/")) .andExpect(model().attribute("integer", equalTo(3))) .andExpect(model().attribute("string", allOf(startsWith("a string"), endsWith("value")))) .andExpect(model().attribute("person", hasProperty("name", equalTo("a name")))); }
@Test void springMvcTest(WebApplicationContext wac) throws Exception { webAppContextSetup(wac).build() .perform(get("/person/42").accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(jsonPath("$.name", is("Dilbert"))); }
@Test // SPR-13079 public void deferredResultWithDelayedError() throws Exception { MvcResult mvcResult = this.mockMvc.perform(get("/1").param("deferredResultWithDelayedError", "true")) .andExpect(request().asyncStarted()) .andReturn(); this.mockMvc.perform(asyncDispatch(mvcResult)) .andExpect(status().is5xxServerError()) .andExpect(content().string("Delayed Error")); }
@Test public void testRequestAttributeEqualTo() throws Exception { this.mockMvc.perform(get("/main/1").servletPath("/main")) .andExpect(request().attribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE, "/{id}")) .andExpect(request().attribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "/1")) .andExpect(request().attribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE, equalTo("/{id}"))) .andExpect(request().attribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, equalTo("/1"))); }
@Test public void multipartRequestWithSingleFileNotPresent() throws Exception { standaloneSetup(new MultipartController()).build() .perform(multipart("/multipartfile")) .andExpect(status().isFound()); }
@Test public void multipartRequestWithFileListNoMultipart() throws Exception { standaloneSetup(new MultipartController()).build() .perform(post("/multipartfilelist")) .andExpect(status().isFound()); }
@Test public void deferredResult() throws Exception { MvcResult mvcResult = this.mockMvc.perform(get("/1").param("deferredResult", "true")) .andExpect(request().asyncStarted()) .andReturn(); this.asyncController.onMessage("Joe"); this.mockMvc.perform(asyncDispatch(mvcResult)) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8)) .andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}")); }
@Test public void defaultViewResolver() throws Exception { standaloneSetup(new PersonController()).build() .perform(get("/person/Corea")) .andExpect(model().attribute("person", hasProperty("name", equalTo("Corea")))) .andExpect(status().isOk()) .andExpect(forwardedUrl("person/show")); // InternalResourceViewResolver }
@Test public void queryParameter() throws Exception { standaloneSetup(new PersonController()).build() .perform(get("/search?name=George").accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(content().contentType("application/json;charset=UTF-8")) .andExpect(jsonPath("$.name").value("George")); }
@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)); }