【发布时间】:2010-03-24 09:35:19
【问题描述】:
我需要为在 Tomcat 中运行的相当复杂的应用程序编写一个 jUnit 测试。
我写了一个类来构建我的 Spring 上下文。
private static ApplicationContext
springContext = null;
springContext = new ClassPathXmlApplicationContext(
new String[] {"beans"....});
在应用程序中有一个调用:
public class myClass implements ServletContextAware {
.... final String folder = **servletContext.getRealPath**("/example"); ...
}
这会破坏一切,因为 ServletContext 为空。
我已经开始构建一个模拟对象:
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);
}
是否有一种简单的方法可以在 jUnit 测试运行时注入 ServletContext 或使用部署描述符启动 Tomcat?
我正在使用:Spring、Maven、Tomcat 6 和 EasyMock 来模拟对象。
【问题讨论】:
标签: java unit-testing spring servlets junit