【发布时间】: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