users@jersey.java.net

[Jersey] Unit Testing Sub Resources

From: Mark Engel <mark.c.engel_at_gmail.com>
Date: Sat, 9 Apr 2011 14:32:29 +0200

I started to unit test my jersey interface with mockito and
JerseyTest. So far this works like a charm.

However I don't know how to unit test if the correct sub-resource is loaded.
This is an example unit test I've written.
The SubResource is mocked, but is still dependant on the
implementation of SubResource (returns 204 when SubResource has a get
method and 405 when SubResource has no get Method)

What is the prefered way to unit test whether the desired sub resource
is loaded?

public class SubResourceTest extends JerseyTest {

        // the resource which is unit tested
        @Path("/")
        public static class Resource {

                public Resource() {
                }

                @Path("sub")
                public SubResource getSub() {
                        return sub;
                }
        }

        public class SubResource {
                @GET
                public String foo() {
                        return "foo";
                }
        }


        private static SubResource sub;

        public SubResourceTest() {
                super();
        }


        @Override
        protected AppDescriptor configure() {
                setTestContainerFactory(new GrizzlyTestContainerFactory());
                return new LowLevelAppDescriptor.Builder(Resource.class).build();
        }

        @Before
        @Override
        public void setUp() throws Exception {
                sub = mock(SubResource.class);
                super.setUp();
        }

        @Test
        public void testGetCustomerResource() {
                // returns a 204 no_content, if SubResource has a get Method
                // returns a 405 Method not allowed, if the SubResource has no get Method
                SubResource actual = resource().path("/sub").get(SubResource.class);
                //assertEquals(customerResource, actual);
        }
}