dev@jersey.java.net

Unit test Jersey with Spring 3.0

From: Choon-Chern Lim \(Mike\) <"Choon-Chern>
Date: Wed, 12 May 2010 12:23:36 -0700 (PDT)

I have been using Jersey with Spring 2.5.6 all along, and all my testcases run fine in Grizzly.

I decided to upgrade the Spring version to 3.0. Since the latest Jersey is still dependent on 2.5.6, I decided to swap that out.

When I run my unit test in Grizzly, I get the following exception:-

==============================
SEVERE: service exception:
java.lang.IllegalStateException: Failed to invoke Servlet 2.5 getContextPath method
        at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:268)
        at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:197)
...
...
Caused by: java.lang.NoSuchMethodException: javax.servlet.ServletContext.getContextPath()
        at java.lang.Class.throwNoSuchMethodException(Class.java:274)
        at java.lang.Class.getMethod(Class.java:813)
        at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:263)
        ... 40 more
==============================

I thought Grizzly 1.9.18-i uses Servlet 2.5. The exception occurs when I have the following code in my testcase to enable Spring's injection:-

==============================
// in constructor

super(new WebAppDescriptor.Builder("test")
        .contextPath("ctx")
        .contextParam("contextConfigLocation", "classpath:test-applicationContext.xml")
        .filterClass(OpenSessionInViewFilter.class)
        .contextListenerClass(ContextLoaderListener.class)
        .servletClass(SpringServlet.class)
        .build());
==============================

Here's a snippet of my pom.xml file, and as you can see, I excluded all the spring 2.5.6 libraries here because I added Spring 3.0 (not shown here).

==============================
        <dependency>
                <groupId>com.sun.jersey.jersey-test-framework</groupId>
                <artifactId>jersey-test-framework-grizzly</artifactId>
                <version>1.2</version>
        </dependency>

        <dependency>
                <groupId>com.sun.jersey.contribs</groupId>
                <artifactId>jersey-spring</artifactId>
                <version>1.2</version>
                <exclusions>
                        <exclusion>
                                <artifactId>spring-core</artifactId>
                                <groupId>org.springframework</groupId>
                        </exclusion>
                        <exclusion>
                                <artifactId>spring-beans</artifactId>
                                <groupId>org.springframework</groupId>
                        </exclusion>
                        <exclusion>
                                <artifactId>spring-context</artifactId>
                                <groupId>org.springframework</groupId>
                        </exclusion>
                        <exclusion>
                                <artifactId>spring-web</artifactId>
                                <groupId>org.springframework</groupId>
                        </exclusion>
                        <exclusion>
                                <artifactId>spring</artifactId>
                                <groupId>org.springframework</groupId>
                        </exclusion>
                </exclusions>
        </dependency>
==============================

How do I fix this Servlet 2.5 exception in my unit test? Thanks.

Mike