Grails - 在单元测试中模拟 servletContext

发布于 2024-12-11 05:19:44 字数 438 浏览 5 评论 0原文

我很难在单元测试中模拟 servletContext 。这就是我尝试执行此操作的方法:

import org.springframework.mock.web.MockServletContext
    void testSomehing() {
      def servletContext = new MockServletContext()
      servletContext.numberOfCreditProvider = 2
      [...]
    }

我收到以下错误消息:

No such property: numberOfCreditProvider for class: org.springframework.mock.web.MockServletContext

非常感谢任何帮助。

I am having hard time mocking servletContext in a unit test. This is how I try to do it:

import org.springframework.mock.web.MockServletContext
    void testSomehing() {
      def servletContext = new MockServletContext()
      servletContext.numberOfCreditProvider = 2
      [...]
    }

I get the following error message:

No such property: numberOfCreditProvider for class: org.springframework.mock.web.MockServletContext

Any help greatly apreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

草莓味的萝莉 2024-12-18 05:19:44

尝试

 servletContext.setAttribute("numberOfCreditProvider", 2)

Try

 servletContext.setAttribute("numberOfCreditProvider", 2)
桜花祭 2024-12-18 05:19:44

就其价值而言,似乎可以在单元测试和集成测试中设置属性,但属性只能在集成测试中设置。这种类似地图的功能似乎在单元测试中不起作用。

For what it's worth, it seems that it's possible to set attributes in both unit and integration tests, but properties can only be set in integration tests. That map-like ability does not seem to work in unit tests.

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