jdevelop
jdevelop

Reputation: 12296

Starting Jetty with Spring context in tests

I need to get Jetty server up and running at the beginning of the test. The Jetty server has to pick up Spring context and start set of RESTful services. Then I need to take the context used by Jetty and inject some mock implementation of service into certain service provider bean so I can test that service gets correct request and response and authenticates as well.

I created simple bootstrap class:

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...
    }
}

However when launching this unit-test, I'm getting the following error:

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

So I doubt what may be wrong here and is the approach I had chosen right one?

What do I need to to in order to get Jetty up and launch Spring context? And how to get reference to the Spring context after than in test body, so I can get service bean and invoke some setters with mocked sub-service?

Please advice.

I don't need to create unit tests - it's something like end-to-end test with mocks.

Upvotes: 0

Views: 4885

Answers (1)

jeha
jeha

Reputation: 10710

The method javax.servlet.ServletContext.getContextPath() does not exist (and hence cannot be called).

This would mean you have a old servlet-X.jar in your classpath. The method getContextPath() is since Servlet 2.5 so you need at least 2.5. Check and fix your dependencies (especially transitive dependencies) in this respect.

Upvotes: 4

Related Questions