对依赖于 Spring 的 WebApplicationContextUtils.getRequiredWebApplicationContext(context) 的 Servlet 进行单元测试

发布于 2024-12-20 18:59:30 字数 705 浏览 3 评论 0原文

我想对依赖 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绮筵 2024-12-27 18:59:30

您可以通过以下方式注入应用程序上下文:

getServletContext().setAttribute(
  WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
  testApplicationContext
);

这是有效的,因为 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:

getServletContext().setAttribute(
  WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
  testApplicationContext
);

This works because WebApplicationContextUtils fetches an object stored in ServletContext with org.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).

纸伞微斜 2024-12-27 18:59:30

我必须使用此静态 WebApplicationContextUtils.getRequiredWebApplicationContext 实用程序方法对访问应用程序上下文的 servlet 过滤器进行单元测试。

@Tomasz Nurkiewicz 之前的回答帮助我正确模拟了测试上下文。由于答案不包含实际代码,我想分享我的实现,以防将来对某人有用。

WebApplicationContext applicationContext = mock(WebApplicationContext.class);
HttpSession httpSession = mock(HttpSession.class);
ServletContext servletContext = mock(ServletContext.class);
when(httpSession.getServletContext()).thenReturn(servletContext);
when(servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE))
    .thenReturn(applicationContext);
when(httpServletRequest.getSession()).thenReturn(httpSession);

在过滤器类本身中,我使用实用程序方法检索上下文。

ApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(
    httpServletRequest.getSession().getServletContext());

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.

WebApplicationContext applicationContext = mock(WebApplicationContext.class);
HttpSession httpSession = mock(HttpSession.class);
ServletContext servletContext = mock(ServletContext.class);
when(httpSession.getServletContext()).thenReturn(servletContext);
when(servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE))
    .thenReturn(applicationContext);
when(httpServletRequest.getSession()).thenReturn(httpSession);

And in the filter class itself, I retrieve the context with the utility method.

ApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(
    httpServletRequest.getSession().getServletContext());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文