【问题标题】:I've injected HttpServletRequest into a bean. How do I unit test it?我已经将 HttpServletRequest 注入到一个 bean 中。我如何对其进行单元测试?
【发布时间】:2011-04-04 09:43:44
【问题描述】:

我有一个 bean,其中有 I've injected an HttpServletRequest using the @Autowired annotation。

当应用程序上下文是 Web 应用程序上下文时,此注入可以正常工作。对于使用 Spring 进行 JUnit 测试的应用程序上下文,情况并非如此。

如何测试这个 bean?也许我可以模拟一个 http 请求,但是如何在 bean 中注入这个模拟?

这是在 Spring 3.0 和 Junit 4.4 上

【问题讨论】:

    标签: spring junit


    【解决方案1】:

    创建一个MockHttpServletRequest 类型的bean 并将其添加到您的测试上下文中。然后应该将其自动装配到您的目标 bean 中。

    【讨论】:

    • 谢谢,但是如何“将其添加到您的测试上下文中”? Here 伙计们还没有找到解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多