【问题标题】:Starting Jetty with Spring context in tests在测试中使用 Spring 上下文启动 Jetty
【发布时间】:2011-11-21 22:01:23
【问题描述】:

我需要在测试开始时启动并运行 Jetty 服务器。 Jetty 服务器必须获取 Spring 上下文并启动一组 RESTful 服务。然后我需要获取 Jetty 使用的上下文并将一些服务的模拟实现注入到某些服务提供者 bean 中,以便我可以测试该服务是否获得正确的请求和响应并进行身份验证。

我创建了简单的引导类:

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class RESTServiceTest {

    @BeforeClass
    public static void setUp() throws Exception {
        Server server = new Server(8080);
        WebAppContext ctx = new WebAppContext();
        ctx.setContextPath("/");
        ctx.setWar("src/main/webapp");
        ctx.setServer(server);
        server.setHandler(ctx);
        server.start();
        Thread.sleep(5000);
    }

    @Test
    public void testPush() throws Exception {
        // do some tests here...
    }
}

但是在启动此单元测试时,我收到以下错误:

Context initialization failed
java.lang.IllegalStateException: Failed to invoke Servlet 2.5 getContextPath method
at  org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:365)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:278)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
at org.eclipse.jetty.server.handler.ContextHandler.startContext(ContextHandler.java:640)
at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:229)
at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1208)
at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:586)
at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:449)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:58)
at org.eclipse.jetty.server.handler.HandlerWrapper.doStart(HandlerWrapper.java:89)
at org.eclipse.jetty.server.Server.doStart(Server.java:258)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:58)
....
Caused by: java.lang.NoSuchMethodException: javax.servlet.ServletContext.getContextPath()
at java.lang.Class.getMethod(Class.java:1622)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:360)
... 30 more

所以我怀疑这里可能出了什么问题,我选择的方法是否正确?

为了启动 Jetty 并启动 Spring 上下文,我需要做什么?以及如何在测试主体之后获取对 Spring 上下文的引用,以便我可以获取服务 bean 并使用模拟的子服务调用一些设置器?

请指教。

我不需要创建单元测试 - 它类似于带有模拟的端到端测试。

【问题讨论】:

    标签: java spring testing servlets embedded-jetty


    【解决方案1】:

    javax.servlet.ServletContext.getContextPath() 方法不存在(因此无法调用)。

    这意味着您的类路径中有一个旧的servlet-X.jargetContextPath() 方法是从 Servlet 2.5 开始的,因此您至少需要 2.5。在这方面检查并修复您的依赖项(尤其是传递依赖项)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-09
      • 2017-08-21
      • 1970-01-01
      • 2022-01-18
      • 1970-01-01
      • 1970-01-01
      • 2018-08-27
      相关资源
      最近更新 更多