如何在 Spring 单元测试中注入 ServletContext?
我需要为在 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 为 null。
我已经开始构建一个模拟对象:
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);
}
是否有一个简单的方法来注入 ServletContext 或在 jUnit 测试运行时使用部署描述符启动 Tomcat?
我使用 Spring、Maven、Tomcat 6 和 EasyMock 作为模拟对象。
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 method 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你真正想要的是测试网络层。
有几种方法可以做到这一点:
MockServletContext
由spring提供。这是最好的方法 - 检查链接文档以了解如何操作。每当您想在测试中注入某些内容时,请使用
ReflectionTestUtils
复杂性来自于 Web 层不太适合单元测试这一事实。它更多的是功能测试的主题。
如果有一些方法看起来适合单元测试,那么它们可能属于服务层。并且它们不应该依赖于
ServletContext
What you actually want is to test the web-layer.
There are a couple of ways to do it:
MockServletContext
provided by spring. This is the best way - check the linked documentation for how to do it.And whenever you want to inject something in a test, use
ReflectionTestUtils
The complication comes from the fact that the web-layer is not quite suitable for unit-testing. It is more a subject of functional testing.
If there are methods that seem suitable for unit-testing, they perhaps belong to the service layer. And they should not have a dependency on the
ServletContext