Java Code Examples for org.springframework.mock.web.MockServletContext

Following code examples demonstrate how to use org.springframework.mock.web.MockServletContextfrom spring-framework. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use org.springframework.mock.web.MockServletContextand various code implementation of this class.

    public void testMockServletContext() {
        MockServletContext servletContext = new MockServletContext();

        servletContext.setInitParameter("abc", "def");


    } 


    public static GrailsWebRequest bindMockWebRequest() {
        ServletContext servletContext = new MockServletContext();
        MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
        MockHttpServletResponse response = new MockHttpServletResponse();
        return bindMockWebRequest(servletContext, request, response);
    } 

Advertisement
Javadoc
Mock implementation of the javax.servlet.ServletContext interface.

As of Spring 5.0, this

set of mocks is designed on a Servlet 4.0 baseline.

Compatible with Servlet 3.1 but can be configured to expose a specific version through #setMajorVersion/#setMinorVersion; default is 3.1. Note that Servlet 3.1 support is limited: servlet, filter and listener registration methods are not supported; neither is JSP configuration. We generally do not recommend to unit test your ServletContainerInitializers and WebApplicationInitializers which is where those registration methods would be used.

For setting up a full WebApplicationContext in a test environment, you can use AnnotationConfigWebApplicationContext, XmlWebApplicationContext, or GenericWebApplicationContext, passing in a corresponding MockServletContext instance. Consider configuring your MockServletContext with a FileSystemResourceLoader in order to interpret resource paths as relative filesystem locations. @author Rod Johnson @author Juergen Hoeller @author Sam Brannen @since 1.0.2 @see #MockServletContext(org.springframework.core.io.ResourceLoader) @see org.springframework.web.context.support.AnnotationConfigWebApplicationContext @see org.springframework.web.context.support.XmlWebApplicationContext @see org.springframework.web.context.support.GenericWebApplicationContext

Read More
Advertisement