dev@jersey.java.net

Unit test with inmemory server and spring

From: Choon-Chern Lim \(Mike\) <"Choon-Chern>
Date: Thu, 13 May 2010 10:59:18 -0700 (PDT)

Does test framework's inmemory server support Spring injection?

My simple resource class:-
==========================================
@Path("/hello")
@Component
public class HelloResource {

        @Inject
        private SomeService someService;

        @GET
        public String getMessage() {
                return someService.run();
        }
}
==========================================

My testcase:-
==========================================
public class CodeTest extends JerseyTest {
        public CodeTest() {
                super(new WebAppDescriptor.Builder("test.resource")
                        .contextPath("testJersey")
                        .contextParam("contextConfigLocation","classpath:applicationContext.xml")
                        .contextListenerClass(ContextLoaderListener.class)
                        .servletClass(SpringServlet.class).build());
        }

        @Test
        public void testCode() {
                assertEquals("hello", resource().path("/hello").getRequestBuilder().get(String.class));
        }
}
==========================================

When I execute this testcase in Grizzly, everything works fine.

when I switch Grizzly to inmemory server, I get this exception:-

==========================================
Caused by: java.lang.InstantiationException: test.service.SomeService
==========================================

After futher inspection, it seems like the inmemory server doesn't load my applicationContext.xml file.

I have the following in my pom.xml file:-
==========================================
<dependency>
        <groupId>com.sun.jersey.contribs</groupId>
        <artifactId>jersey-spring</artifactId>
        <version>1.2-SNAPSHOT</version>
</dependency>

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

I swapped grizzly with inmemory, and my testcase fails:-
==========================================
<dependency>
        <groupId>com.sun.jersey.jersey-test-framework</groupId>
        <artifactId>jersey-test-framework-inmemory</artifactId>
        <version>1.2-SNAPSHOT</version>
</dependency>
==========================================

Is this a limitation in using inmemory server? Or do I need to change my test configuration to get the Spring injection to work?

Thanks.

CC