【问题标题】:realPath for MockHttpServletRequestMockHttpServletRequest 的 realPath
【发布时间】:2012-04-23 07:03:24
【问题描述】:

我正在为我的 Spring 控制器编写一个基于 Spring 的测试用例,在我的控制器中我们使用的是这样的 "request.getSession().getServletContext().getRealPath("/myFolderName");" 它对正常请求工作正常,但对于像 MockHttpServletRequest 这样的单元测试用例,上面的方法调用给出了 null。如何获取 MockHttpServletRequest 的 realPath。

谢谢, 普拉内斯。

【问题讨论】:

  • 将语句拆分为几个较小的语句,以便您查看导致 NPE 的部分。或者使用调试器。

标签: spring realpath


【解决方案1】:

默认情况下,MockHttpServletRequest 就像根 webapp 文件夹是类路径一样,因此您应该能够将 getRealPath() 用于类路径资源。

或者,您可以为MockHttpServletRequest 提供MockServletContext,并且MockServletContext 可以为getRealPath() 配置特定的根文件夹等。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-04
    • 2011-05-02
    • 1970-01-01
    • 1970-01-01
    • 2016-09-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多