/** * Create a new instance using the supplied {@link WebApplicationContext}. * @param context the {@code WebApplicationContext} to create a {@code MockMvc} * instance from; never {@code null} */ protected MockMvcWebConnectionBuilderSupport(WebApplicationContext context) { this(MockMvcBuilders.webAppContextSetup(context).build()); }
/** * Create a new instance using the supplied {@link WebApplicationContext} * and {@link MockMvcConfigurer}. * @param context the {@code WebApplicationContext} to create a {@code MockMvc} * instance from; never {@code null} * @param configurer the MockMvcConfigurer to apply; never {@code null} */ protected MockMvcWebConnectionBuilderSupport(WebApplicationContext context, MockMvcConfigurer configurer) { this(MockMvcBuilders.webAppContextSetup(context).apply(configurer).build()); }
@Before public void setup() { this.mockMvc = webAppContextSetup(this.wac).build(); }
@Before public void setup() { this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); }
@Before public void setup() { this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); }
@Before public void setUp() { this.mvc = webAppContextSetup(this.wac).build(); }
@Test public void webAppContextSetupWithNullWac() { exception.expect(IllegalArgumentException.class); exception.expectMessage(equalTo("WebApplicationContext is required")); webAppContextSetup(null); }
@Test public void webAppContextSetupWithNullServletContext() { exception.expect(IllegalArgumentException.class); exception.expectMessage(equalTo("WebApplicationContext must have a ServletContext")); webAppContextSetup(new StubWebApplicationContext(null)); }
@Before public void setup() { this.mockMvc = webAppContextSetup(this.wac).dispatchOptions(true).build(); }
@Test public void mockMvc() throws Exception { MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(wac).build(); WebConnection conn = new MockMvcWebConnectionBuilderSupport(mockMvc) {}.createConnection(this.client); assertMockMvcUsed(conn, "http://localhost/"); assertMockMvcNotUsed(conn, "http://example.com/"); }
@Before public void setup() { this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).alwaysExpect(status().isOk()).build(); }
/** * See SPR-12553 and SPR-13075. */ @Test public void rootWacServletContainerAttributePreviouslySet() { StubWebApplicationContext child = new StubWebApplicationContext(this.servletContext); this.servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, child); DefaultMockMvcBuilder builder = webAppContextSetup(child); assertSame(builder.initWebAppContext(), WebApplicationContextUtils.getRequiredWebApplicationContext(this.servletContext)); }
@Before public void setup() { this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); given(this.personDao.getPerson(5L)).willReturn(new Person("Joe")); }
/** * See SPR-12553 and SPR-13075. */ @Test public void rootWacServletContainerAttributeNotPreviouslySet() { StubWebApplicationContext root = new StubWebApplicationContext(this.servletContext); DefaultMockMvcBuilder builder = webAppContextSetup(root); WebApplicationContext wac = builder.initWebAppContext(); assertSame(root, wac); assertSame(root, WebApplicationContextUtils.getRequiredWebApplicationContext(this.servletContext)); }
@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 chainMultiple() { MockMvcBuilders .webAppContextSetup(wac) .addFilter(new CharacterEncodingFilter() ) .defaultRequest(get("/").contextPath("/mywebapp")) .build(); }
@Before public void setup() { this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); verifyRootWacSupport(); given(this.personDao.getPerson(5L)).willReturn(new Person("Joe")); }
@BeforeEach void setUpMockMvc(WebApplicationContext wac) { this.mockMvc = webAppContextSetup(wac) .alwaysExpect(status().isOk()) .alwaysExpect(content().contentTypeCompatibleWith(APPLICATION_JSON)) .build(); }
@Test public void dispatcherServletCustomizerProcessedInOrder() { StubWebApplicationContext root = new StubWebApplicationContext(this.servletContext); DefaultMockMvcBuilder builder = webAppContextSetup(root); builder.addDispatcherServletCustomizer(ds -> ds.setContextId("test-id")); builder.addDispatcherServletCustomizer(ds -> ds.setContextId("override-id")); builder.dispatchOptions(true); MockMvc mvc = builder.build(); DispatcherServlet ds = (DispatcherServlet) new DirectFieldAccessor(mvc) .getPropertyValue("servlet"); assertEquals("override-id", ds.getContextId()); }
@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(); }