【问题标题】:JerseyTest and Spring and creating a ServletContextJerseyTest 和 Spring 并创建一个 ServletContext
【发布时间】:2014-03-26 21:15:24
【问题描述】:

我正在努力从 Jersey 1.16 迁移到 Jersey 2.7。我的应用程序正在运行,但我在测试时遇到了问题。

注意事项:
该应用程序使用 Spring 并由一个类 ApplicationConfiguration.class 进行配置。 该应用程序没有 web.xml - Jersey servlet 和过滤器以编程方式配置。 我正在使用 jersey-spring3 库。 与使用 jersey-spring3 库相关,我必须在我的 onStartup 方法中添加一个解决方法

// The following line is required to avoid having jersey-spring3 registering it's own Spring root     context.
// https://java.net/jira/browse/JERSEY-2038
servletContext.setInitParameter("contextConfigLocation", "");

问题是这样的:
当测试开始时,SpringComponentProvider 会尝试用一个危险的假设来初始化 Spring,我无法弄清楚如何更正 - 基于 xml 的配置。看代码,麻烦的是这个块

    ServletContext sc = locator.getService(ServletContext.class);

    if(sc != null) {
        // servlet container
        ctx = WebApplicationContextUtils.getWebApplicationContext(sc);
    } else {
        // non-servlet container
        ctx = createSpringContext();
    }

运行JUnit测试,ServletContext为null,调用createSpringContext。

问题来了:
有没有办法运行测试并指定 ServletContext/ServletContainer?

【问题讨论】:

    标签: spring jersey jersey-2.0


    【解决方案1】:

    我相信https://java.net/jira/browse/JERSEY-2259 涵盖了这个问题。

    简而言之:他们从 Jersey 2.x 中删除了此功能,并将其视为功能请求(而不是回归),因此它不被视为高优先级项目。

    【讨论】:

      猜你喜欢
      • 2011-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-23
      • 2013-11-12
      • 1970-01-01
      • 2013-09-25
      • 2014-11-12
      相关资源
      最近更新 更多