@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 tilesDefinitions() throws Exception { this.mockMvc.perform(get("/")) .andExpect(status().isOk()) .andExpect(forwardedUrl("/WEB-INF/layouts/standardLayout.jsp")); }
@BeforeEach void setUpMockMvc(WebApplicationContext wac) { this.mockMvc = webAppContextSetup(wac) .alwaysExpect(status().isOk()) .alwaysExpect(content().contentTypeCompatibleWith(APPLICATION_JSON)) .build(); }
@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 testExceptionHandlerMethod() throws Exception { standaloneSetup(new PersonController()).build() .perform(get("/person/Clyde")) .andExpect(status().isOk()) .andExpect(forwardedUrl("errorView")); }
@Test public void person() throws Exception { this.mockMvc.perform(get("/person/5").accept(MediaType.APPLICATION_JSON)) .andDo(print()) .andExpect(status().isOk()) .andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}")); }
@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.mockRequest.setAttribute(FROM_TCF_MOCK, FROM_TCF_MOCK); this.mockMvc = webAppContextSetup(this.wac) .addFilters(new RequestFilter(), new RequestAttributesFilter(), this.filterWithSessionScopedService) .defaultRequest(get("/").requestAttr(FROM_MVC_TEST_DEFAULT, FROM_MVC_TEST_DEFAULT)) .alwaysExpect(status().isOk()) .build(); }
@Test public void streaming() throws Exception { this.mockMvc.perform(get("/1").param("streaming", "true")) .andExpect(request().asyncStarted()) .andDo(MvcResult::getAsyncResult) // fetch async result similar to "asyncDispatch" builder .andExpect(status().isOk()) .andExpect(content().string("name=Joe")); }
@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")); }
@Before public void setup() { CookieLocaleResolver localeResolver = new CookieLocaleResolver(); localeResolver.setCookieDomain("domain"); localeResolver.setCookieHttpOnly(true); this.mockMvc = standaloneSetup(new SimpleController()) .addInterceptors(new LocaleChangeInterceptor()) .setLocaleResolver(localeResolver) .defaultRequest(get("/").param("locale", "en_US")) .alwaysExpect(status().isOk()) .build(); }
@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 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 testJspOnly() throws Exception { InternalResourceViewResolver viewResolver = new InternalResourceViewResolver("/WEB-INF/", ".jsp"); standaloneSetup(new PersonController()).setViewResolvers(viewResolver).build() .perform(get("/person/Corea")) .andExpect(status().isOk()) .andExpect(model().size(1)) .andExpect(model().attributeExists("person")) .andExpect(forwardedUrl("/WEB-INF/person/show.jsp")); }
@Test // SPR-12597 public void completableFutureWithImmediateValue() throws Exception { MvcResult mvcResult = this.mockMvc.perform(get("/1").param("completableFutureWithImmediateValue", "true")) .andExpect(request().asyncStarted()) .andReturn(); 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 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 listenableFuture() throws Exception { MvcResult mvcResult = this.mockMvc.perform(get("/1").param("listenableFuture", "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}")); }