对依赖于 Spring 的 WebApplicationContextUtils.getRequiredWebApplicationContext(context) 的 Servlet 进行单元测试
我想对依赖 Spring 的 init()
方法中的 WebApplicationContextUtils.getRequiredWebApplicationContext(context)
的 servlet 代码进行单元测试。
这是代码的一部分:
@Override
public void init() throws ServletException {
super.init();
WebApplicationContext applicationContext =
WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
this.injectedServiceBean = (SomeService) applicationContext.getBean("someBean");
}
将正确的 applicationContext.xml (测试版本)注入此文本的最佳方法是什么?
我知道 Spring 的 @ContextConfiguration,但我不确定注入加载的 ${testClass}Test-context.xml
上下文的最佳方法通过将该注释添加到 servlet 上下文中,以便 getRequiredWebApplicationContext(...) 可以返回它。
I'd like to unit-test a servlet code that relies on Spring's WebApplicationContextUtils.getRequiredWebApplicationContext(context)
in its init()
method.
Here is part of the code:
@Override
public void init() throws ServletException {
super.init();
WebApplicationContext applicationContext =
WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
this.injectedServiceBean = (SomeService) applicationContext.getBean("someBean");
}
What would be the best way to inject a proper applicationContext.xml (test version) into this text?
I'm aware of Spring's @ContextConfiguration
, but it I'm not sure about the best approach to inject the ${testClass}Test-context.xml
context that is loaded by that annotation into the servlet context, so that the getRequiredWebApplicationContext(...) can return it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过以下方式注入应用程序上下文:
这是有效的,因为
WebApplicationContextUtils
使用org.springframework.web.context.WebApplicationContext.ROOT< 获取存储在
ServletContext
中的对象/code> 键(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE
常量)。这仅表明直接从应用程序上下文获取 bean 是有问题的,因为这种方法不遵循 DI 规则。如果可以的话,尝试重构这个 servlet 以更好地与 Spring 集成(例如使用
HttpRequestHandlerServlet
参见示例)。You can inject application context in the following way:
This works because
WebApplicationContextUtils
fetches an object stored inServletContext
withorg.springframework.web.context.WebApplicationContext.ROOT
key (WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE
constant).This only shows that fetching beans directly from an application context is problematic since this approach does not follow DI rules. If you can, try refactoring this servlet to integrate it better with Spring (e.g. using
HttpRequestHandlerServlet
see example).我必须使用此静态
WebApplicationContextUtils.getRequiredWebApplicationContext
实用程序方法对访问应用程序上下文的 servlet 过滤器进行单元测试。@Tomasz Nurkiewicz 之前的回答帮助我正确模拟了测试上下文。由于答案不包含实际代码,我想分享我的实现,以防将来对某人有用。
在过滤器类本身中,我使用实用程序方法检索上下文。
I had to unit test a servlet filter that accesses application context using this static
WebApplicationContextUtils.getRequiredWebApplicationContext
utility method.The previous answer from @Tomasz Nurkiewicz helped me to properly mock the context for tests. As the answer doesn't contain the actual code, I'd like to share my implementation in case it'll be useful for someone in the future.
And in the filter class itself, I retrieve the context with the utility method.