Page 2 of 2 FirstFirst 12
Results 11 to 14 of 14

Thread: Accessing WebApplicationContext from TestContext

  1. #11

    Default

    How about this?
    in your test:
    Code:
    @RunWith(.....
    @ContextConfiguration(loader=MyContextLoader.class, locations={"classpath:my-application-context.xml"})
    public class DelegatingTagLibProxyTest {
        // use 
        // WebApplicationContextUtils.getRequiredWebApplicationContext(MyContextLoader.SERVLET_CONTEXT)
        //
    }
    and the contextLoader...

    Code:
    public class MyContextLoader extends AbstractContextLoader {
    
    	protected static final Log logger = LogFactory.getLog(MyContextLoader.class);
    	
    	public static final ServletContext SERVLET_CONTEXT = new MockServletContext();
    	
    	protected BeanDefinitionReader createBeanDefinitionReader(final GenericApplicationContext context) {
    		return new XmlBeanDefinitionReader(context);
    	}
    	
    	public final ConfigurableApplicationContext loadContext(final String... locations) throws Exception {
    
    		if (logger.isDebugEnabled()) {
    			logger.debug("Loading ApplicationContext for locations ["
    					+ StringUtils.arrayToCommaDelimitedString(locations) + "].");
    		}
    
    		final GenericWebApplicationContext webContext = new GenericWebApplicationContext();
    		servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, webContext);
    		webContext.setServletContext(SERVLET_CONTEXT);
    		createBeanDefinitionReader(webContext).loadBeanDefinitions(locations);
    		AnnotationConfigUtils.registerAnnotationConfigProcessors(webContext);
    		webContext.refresh();
    		webContext.registerShutdownHook();
    		return webContext;
    	}
    	
    	@Override
    	protected String getResourceSuffix() {
    		return "-context.xml";
    	}
    
    }

  2. #12
    Join Date
    Oct 2008
    Posts
    8

    Default

    Hi JuanManual32

    Many thanks for your help - this seems to do the trick.The loader makes the WebApplicationContext available resolving both the XmlViewResolver and RequestContext errors I was seeing and the dependancyinjection works.

    Here's your ContextLoader with a couple of tweaks :
    Code:
    public class MyContextLoader extends AbstractContextLoader {
        protected static final Log logger = LogFactory.getLog(MyContextLoader.class);
    	
    	public static final ServletContext SERVLET_CONTEXT = new MockServletContext("/WebContent", new FileSystemResourceLoader());
    	
    	protected BeanDefinitionReader createBeanDefinitionReader(final GenericApplicationContext context) {
    		return new XmlBeanDefinitionReader(context);
    	}
    	
    	public final ConfigurableApplicationContext loadContext(final String... locations) throws Exception {
    
    		if (logger.isDebugEnabled()) {
    			logger.debug("Loading ApplicationContext for locations ["
    					+ StringUtils.arrayToCommaDelimitedString(locations) + "].");
    		}
    
    		final GenericWebApplicationContext webContext = new GenericWebApplicationContext();
    		SERVLET_CONTEXT.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, webContext);
    		webContext.setServletContext(SERVLET_CONTEXT);
    		createBeanDefinitionReader(webContext).loadBeanDefinitions(locations);
    		AnnotationConfigUtils.registerAnnotationConfigProcessors(webContext);
    		webContext.refresh();
    		webContext.registerShutdownHook();
    		return webContext;
    	}
    	
    	protected String getResourceSuffix() {
    		return "-context.xml";
    	}
    
    }
    and a test snippet .....

    Code:
    @RunWith(.....
    @ContextConfiguration(loader=MyContextLoader.class, locations={"classpath:my-application-context.xml"})
    public class MyControllerTest extends AbstractTransactionalJUnit4SpringContextTests {
    
    	@Autowired
    	protected hplMetricsController hplMetricsController;
    	
    	private ServletContext servletContext;
    	private  MockPageContext pageContext;
    	private  MockServletConfig servletConfig;
    	private  MockHttpSession session;
    	private  MockHttpServletRequest request;
    	private  MockHttpServletResponse response;
            private  WebApplicationContext wac;
    
    	
    	@Test
    	public void setupMocks() throws Exception {
    		wac = WebApplicationContextUtils.getRequiredWebApplicationContext(MyContextLoader.SERVLET_CONTEXT);
    		servletContext = wac.getServletContext();
                    pageContext = new MockPageContext(servletContext);
                    servletConfig = (MockServletConfig) pageContext.getServletConfig();
    		session = (MockHttpSession)pageContext.getSession();		
    		request = (MockHttpServletRequest) pageContext.getRequest();
    		response = (MockHttpServletResponse)pageContext.getResponse();
    Thanks to everyone for their time and assistance....... Geoff

  3. #13

    Default

    Hi, I have tried this solution but for me it can never find the context.xml file.

    I have a file named /webapp/securityContext/xml

    Code:
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(loader=MockServletContextWebContextLoader.class, locations={"/securityContext.xml"})
    And the config loader class.

    Code:
    @Override
    	public final ConfigurableApplicationContext loadContext(String... locations) throws Exception {
    		ConfigurableWebApplicationContext context = new XmlWebApplicationContext();
    		context.setServletContext(new MockServletContext("/webapp", new FileSystemResourceLoader()));
    		context.setConfigLocations(locations);
    		context.refresh();
    		AnnotationConfigUtils.registerAnnotationConfigProcessors((BeanDefinitionRegistry) context.getBeanFactory());
    		context.registerShutdownHook();
    		return context;
    	}
    
    	@Override
    	protected String getResourceSuffix() {
    		System.out.println("USED???????");
    		return "Context.xml";
    	}
    I have tried all sorts of paths, but it never can find the file.

    Anyone know a solution?

    Thanks,
    Coenos

  4. #14
    Join Date
    Jan 2006
    Location
    Zürich, Switzerland
    Posts
    423

    Default

    Hi Coenos,

    By default, the Spring TestContext Framework assumes that resource locations are in the classpath. If you want to reference an XML configuration file in the filesystem, you'll need to use the "file:" prefix.

    If you have a file named /webapp/securityContext.xml, I would assume that means it's in a "webapp" folder within your project. In that case, the following should work:

    Code:
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(
        locations="file:webapp/securityContext.xml",
        loader=MockServletContextWebContextLoader.class)
    If you're using the Maven conventions for folders, your "webapp" folder would be under "src/main/webapp" in your current project, and you should store configuration files somewhere under "WEB-INF" (e.g., "src/main/webapp/WEB-INF/securityContext.xml"). In that case, the following should work:

    Code:
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(
        locations="file:src/main/webapp/WEB-INF/securityContext.xml",
        loader=MockServletContextWebContextLoader.class)

    Hope this helps!

    Sam

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •