Search in sources :

Example 1 with MockMvcFactory

use of io.restassured.module.mockmvc.internal.MockMvcFactory in project rest-assured by rest-assured.

the class RestAssuredMockMvc method webAppContextSetup.

/**
 * 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) {
    // To avoid compile-time errors
    DefaultMockMvcBuilder builder = MockMvcBuilders.webAppContextSetup(context);
    if (mockMvcConfigurers != null && mockMvcConfigurers.length > 0) {
        for (MockMvcConfigurer mockMvcConfigurer : mockMvcConfigurers) {
            builder.apply(mockMvcConfigurer);
        }
    }
    mockMvcFactory = new MockMvcFactory(builder);
}
Also used : DefaultMockMvcBuilder(org.springframework.test.web.servlet.setup.DefaultMockMvcBuilder) MockMvcFactory(io.restassured.module.mockmvc.internal.MockMvcFactory) StandaloneMockMvcFactory(io.restassured.module.mockmvc.internal.StandaloneMockMvcFactory) MockMvcConfigurer(org.springframework.test.web.servlet.setup.MockMvcConfigurer)

Example 2 with MockMvcFactory

use of io.restassured.module.mockmvc.internal.MockMvcFactory in project rest-assured by rest-assured.

the class MockMvcRequestSpecBuilder method getConfiguredMockMvcFactory.

private static MockMvcFactory getConfiguredMockMvcFactory() {
    try {
        Field mockMvcFactory = RestAssuredMockMvc.class.getDeclaredField("mockMvcFactory");
        mockMvcFactory.setAccessible(true);
        Object instance = mockMvcFactory.get(RestAssuredMockMvc.class);
        mockMvcFactory.setAccessible(false);
        return (MockMvcFactory) instance;
    } catch (Exception e) {
        throw new RuntimeException("Internal error: Cannot find mockMvcFactory field in " + RestAssuredMockMvc.class.getName());
    }
}
Also used : Field(java.lang.reflect.Field) MockMvcFactory(io.restassured.module.mockmvc.internal.MockMvcFactory) RestAssuredMockMvc(io.restassured.module.mockmvc.RestAssuredMockMvc)

Aggregations

MockMvcFactory (io.restassured.module.mockmvc.internal.MockMvcFactory)2 RestAssuredMockMvc (io.restassured.module.mockmvc.RestAssuredMockMvc)1 StandaloneMockMvcFactory (io.restassured.module.mockmvc.internal.StandaloneMockMvcFactory)1 Field (java.lang.reflect.Field)1 DefaultMockMvcBuilder (org.springframework.test.web.servlet.setup.DefaultMockMvcBuilder)1 MockMvcConfigurer (org.springframework.test.web.servlet.setup.MockMvcConfigurer)1