Tuesday, July 17, 2012

Mock test for WebApplicationContext

Mock test for WebApplicationContext/WebApplicationContextUtils [get Spring Application Context by Mock ]

protected XmlWebApplicationContext m_webAppContext;
static final String[] CONFIG_FILE_LOCATIONS = { "classpath:datasource.xml", "classpath:applicationContext-test.xml" };
   
m_webAppContext = new XmlWebApplicationContext();        m_webAppContext.setConfigLocations(CONFIG_FILE_LOCATIONS);

// set the servlet context
// Create a new MockServletContext, using no base path and a //DefaultResourceLoader
// (i.e. the classpath root as WAR root).

MockServletContext servletCtx = new MockServletContext();

// the following line is used when fetching the web application //context using this servlet context
// @see //WebApplicationContextUtils#getWebApplicationContext(ServletContext)
           servletCtx.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, m_webAppContext);
m_webAppContext.setServletContext(servletCtx);

// load the beans after injecting dependencies
m_webAppContext.refresh();
m_webAppContext.registerShutdownHook();

UVVK7GDB3MNC

1 comment:

ShareThis