dev@jersey.java.net

Re: [Jersey] Unit test with inmemory server and spring

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Wed, 19 May 2010 17:30:22 +0200

On May 13, 2010, at 7:59 PM, Choon-Chern Lim (Mike) wrote:

> Does test framework's inmemory server support Spring injection?
>

Not directly, you will require a special set up for that to work. As
currently defined you are declaring servlet specific information and
that cannot be abstract to work in non-servlet environments.

I think the test framework should fail under these circumstances
rather than reporting a cryptic error message. The transformation from
a WebAppDescriptor to a LowLevelAppDescriptor
(LowLevelAppDescriptor.transform) should not be possible in this case.
Pavel?

You would need to use a special test configuration based on
InMemoryTestContainerFactory that utilizes the
com.sun.jersey.spi.spring.container.SpringComponentProviderFactory.

Paul.



> 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
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: dev-help_at_jersey.dev.java.net
>