/** * 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()); }
/** * Build a {@link MockMvc} using the given, fully initialized, i.e. * refreshed, {@link WebApplicationContext} and assign it to REST Assured. * The {@link org.springframework.web.servlet.DispatcherServlet} * will use the context to discover Spring MVC infrastructure and * application controllers in it. The context must have been configured with * a {@link javax.servlet.ServletContext}. * * @param context The web application context to use * @param mockMvcConfigurers {@link MockMvcConfigurer}'s to be applied when creating a {@link MockMvc} instance of this WebApplicationContext (optional) */ public static void webAppContextSetup(WebApplicationContext context, MockMvcConfigurer... mockMvcConfigurers) { DefaultMockMvcBuilder builder = MockMvcBuilders.webAppContextSetup(context); // To avoid compile-time errors if (mockMvcConfigurers != null && mockMvcConfigurers.length > 0) { for (MockMvcConfigurer mockMvcConfigurer : mockMvcConfigurers) { builder.apply(mockMvcConfigurer); } } mockMvcFactory = new MockMvcFactory(builder); }
public MockMvcRequestSpecification webAppContextSetup(WebApplicationContext context, MockMvcConfigurer... mockMvcConfigurers) { DefaultMockMvcBuilder builder = MockMvcBuilders.webAppContextSetup(context); if (mockMvcConfigurers != null && mockMvcConfigurers.length > 0) { for (MockMvcConfigurer mockMvcConfigurer : mockMvcConfigurers) { builder.apply(mockMvcConfigurer); } } return changeMockMvcFactoryTo(new MockMvcFactory(builder)); }
@Before public void setup() { // @formatter:off this.mockMvc = MockMvcBuilders .webAppContextSetup(this.context) .addFilters(this.springSessionRepositoryFilter) .apply(springSecurity()) .build(); // @formatter:on }
@Before public void setup() { // @formatter:off this.mockMvc = MockMvcBuilders .webAppContextSetup(this.context) .addFilters(this.springSessionRepositoryFilter) .apply(springSecurity()) .build(); // @formatter:on }
public void autowire() { this.context.setServletContext(new MockServletContext()); this.context.setServletConfig(new MockServletConfig()); this.context.refresh(); if (this.context.containsBean(SPRING_SECURITY_FILTER_CHAIN)) { MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context) .apply(springSecurity()) .apply(new AddFilter()).build(); this.context.getBeanFactory() .registerResolvableDependency(MockMvc.class, mockMvc); } AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); bpp.setBeanFactory(this.context.getBeanFactory()); bpp.processInjection(this.test); }
@BeforeEach void setupWebMvc(ManualRestDocumentation manualRestDocumentation) { FilterChainProxy springSecurityFilterChain = webApplicationContext.getBean("springSecurityFilterChain", FilterChainProxy.class); mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext) .addFilter(springSecurityFilterChain) .apply(documentationConfiguration(manualRestDocumentation) .uris().withPort(80) .and() .snippets() .withTemplateFormat(markdown())) .build(); testClient = new TestClient(mockMvc); } }
@BeforeEach void setUpContext(ManualRestDocumentation manualRestDocumentation) { mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext) .addFilter(springSecurityFilterChain) .apply(documentationConfiguration(manualRestDocumentation) .uris().withPort(80) .and() .snippets() .withTemplateFormat(markdown())) .build(); testClient = new TestClient(mockMvc); }
@Before public void setUpContext() { mockMvc = MockMvcBuilders.webAppContextSetup(this.webApplicationContext) .addFilter(springSecurityFilterChain) .apply(documentationConfiguration(restDocumentation) .uris().withPort(80).and() .snippets() .withTemplateFormat(markdown())) .build(); testClient = new TestClient(mockMvc); }
@Test public void traceWhenDefaultConfigurationThenCsrfIsEnabled() throws Exception { this.spring.configLocations( this.xml("shared-controllers"), this.xml("AutoConfig") ).autowire(); MockMvc traceEnabled = MockMvcBuilders .webAppContextSetup((WebApplicationContext) this.spring.getContext()) .apply(springSecurity()) .addDispatcherServletCustomizer(dispatcherServlet -> dispatcherServlet.setDispatchTraceRequest(true)) .build(); traceEnabled.perform(request(HttpMethod.TRACE, "/csrf-in-header")) .andExpect(csrfInHeader()); }
@Test public void traceWhenCsrfElementEnabledThenOk() throws Exception { this.spring.configLocations( this.xml("shared-controllers"), this.xml("CsrfEnabled") ).autowire(); MockMvc traceEnabled = MockMvcBuilders .webAppContextSetup((WebApplicationContext) this.spring.getContext()) .apply(springSecurity()) .addDispatcherServletCustomizer(dispatcherServlet -> dispatcherServlet.setDispatchTraceRequest(true)) .build(); traceEnabled.perform(request(HttpMethod.TRACE, "/csrf-in-header")) .andExpect(csrfInHeader()); }
@Before public void initMockMvc() { FilterChainProxy springSecurityFilterChain = getWebApplicationContext().getBean("springSecurityFilterChain", FilterChainProxy.class); mockMvc = MockMvcBuilders.webAppContextSetup(getWebApplicationContext()) .addFilter(springSecurityFilterChain) .apply(documentationConfiguration(this.restDocumentation) .uris().withPort(80).and() .snippets() .withTemplateFormat(markdown())) .build(); testClient = new TestClient(mockMvc); }
@BeforeMethod public void setUp(Method method) { this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context) .apply(documentationConfiguration(this.restDocumentation)) .build(); this.restDocumentation.beforeTest(getClass(), method.getName()); }
@BeforeEach public void setUp(WebApplicationContext webApplicationContext, RestDocumentationContextProvider restDocumentation) { this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext) .apply(documentationConfiguration(restDocumentation)) // <1> .build(); } // end::setup[]
@Before public void setup() { this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac) .apply(springSecurity()) .build(); }
@Before public void setUp() { this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context) .apply(documentationConfiguration(this.restDocumentation)) .alwaysDo(document("{method-name}/{step}/")).build(); } // end::parameterized-output[]
@Before public void setUp() { // tag::custom-encoding[] this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context) .apply(documentationConfiguration(this.restDocumentation) .snippets().withEncoding("ISO-8859-1")) .build(); // end::custom-encoding[] }
@Before public void setUp() { // tag::custom-uri-configuration[] this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context) .apply(documentationConfiguration(this.restDocumentation).uris() .withScheme("https") .withHost("example.com") .withPort(443)) .build(); // end::custom-uri-configuration[] }
@Test public void multiPart() throws Exception { MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context) .apply(documentationConfiguration(this.restDocumentation)).build(); mockMvc.perform(multipart("/upload").file("test", "content".getBytes())) .andExpect(status().isOk()).andDo(document("upload", requestParts(partWithName("test").description("Foo")))); }
@Test public void pathParametersSnippet() throws Exception { MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context) .apply(documentationConfiguration(this.restDocumentation)).build(); mockMvc.perform(get("{foo}", "/").accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()).andDo(document("links", pathParameters( parameterWithName("foo").description("The description")))); assertExpectedSnippetFilesExist(new File("build/generated-snippets/links"), "http-request.adoc", "http-response.adoc", "curl-request.adoc", "path-parameters.adoc"); }