@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 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 testNodeHamcrestMatcher() throws Exception { this.mockMvc.perform(get("/music/people")) .andExpect(content().node(hasXPath("/people/composers/composer[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 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 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 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 }
@Before public void setup() { this.mockMvc = standaloneSetup(new MusicController()) .defaultRequest(get("/").accept(MediaType.APPLICATION_JSON)) .alwaysExpect(status().isOk()) .alwaysExpect(content().contentType("application/json;charset=UTF-8")) .build(); }
@Before public void setup() { SampleController controller = new SampleController("a string value", 3, new Person("a name")); this.mockMvc = standaloneSetup(controller) .defaultRequest(get("/")) .alwaysExpect(status().isOk()) .setControllerAdvice(new ModelAttributeAdvice()) .build(); }
@Before public void setup() { this.mockMvc = standaloneSetup(new MusicController()) .defaultRequest(get("/").accept(MediaType.APPLICATION_XML, MediaType.parseMediaType("application/xml;charset=UTF-8"))) .alwaysExpect(status().isOk()) .alwaysExpect(content().contentType(MediaType.parseMediaType("application/xml;charset=UTF-8"))) .build(); }
@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 // 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 testFeedWithLinefeedChars() throws Exception { // Map<String, String> namespace = Collections.singletonMap("ns", ""); standaloneSetup(new BlogFeedController()).build() .perform(get("/blog.atom").accept(MediaType.APPLICATION_ATOM_XML)) .andExpect(status().isOk()) .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_ATOM_XML)) .andExpect(xpath("//feed/title").string("Test Feed")) .andExpect(xpath("//feed/icon").string("http://www.example.com/favicon.ico")); }
@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}")); }