How can i inject servletcontext in spring

Posted by M.R. on Stack Overflow See other posts from Stack Overflow or by M.R.
Published on 2010-03-24T09:35:19Z Indexed on 2010/03/24 9:53 UTC
Read the original article Hit count: 467

Filed under:
|
|
|

I need to write a junit test for a rather complex application which runs in a tomcat.

I wrote a class which builds up my spring context.

private static ApplicationContext springContext = null;

springContext = new ClassPathXmlApplicationContext( new String[] {"beans"....});

In the application there is a call:

public class myClass implements ServletContextAware {

.... final String folder = servletContext.getRealPath("/example"); ... }

which breaks everything, because the ServletContext is null.

I have started to build a mock object:

static ServletConfig servletConfigMock = createMock(ServletConfig.class);

static ServletContext servletContextMock = createMock(ServletContext.class);

@BeforeClass public static void setUpBeforeClass() throws Exception {

expect(servletConfigMock.getServletContext()).andReturn(servletContextMock).anyTimes(); expect(servletContextMock.getRealPath("/example")).andReturn("...fulllpath").anyTimes();
replay(servletConfigMock);

replay(servletContextMock);

}

Is there a simple methode to inject the ServletContext or to start the tomcat with a deployment descriptor at the runtime of the junit test?

I am using: spring, maven, tomcat 6 and easymock for the mock objects.

© Stack Overflow or respective owner

Related posts about servletcontext

Related posts about maven