@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 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 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 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 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 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 testContentStringMatcher() throws Exception { this.mockMvc.perform(get("/handle").accept(MediaType.TEXT_PLAIN)) .andExpect(content().string(containsString("world"))); }
@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 testExists() throws Exception { this.mockMvc.perform(post("/persons")) .andExpect(flash().attributeExists("one", "two", "three")); }
@Test public void testNodeHamcrestMatcher() throws Exception { this.mockMvc.perform(get("/music/people")) .andExpect(content().node(hasXPath("/people/composers/composer[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()); }