users@jersey.java.net

[Jersey] JerseyTest

From: <capable2_at_gmail.com>
Date: Wed, 16 Jan 2013 15:35:54 +0000 (GMT)

Hi,

I'm trying to test one of my Jersey Services. This particular Jersey
Service I've written grabs an attribute out of the HttpSession object
of the HttpServletRequest like so:

                        HttpSession session =
((HttpServletRequest)httpRequest).getSession();
                        userId = (UserId)
session.getAttribute(USER_KEY);

I'm wondering how to mock these values in my JerseyTest so that my
service gets my mocked HttpServletRequest? Here's the code in my test,
but I have no idea how to tell Jersey to use my mocked
HttpServletRequest and HttpSession objects. Can someone throw me a
bone?

        @Test
        public void testGetUserId() {
                try {
                        WebResource webResource = resource();
                        
                        MockHttpSession session = new
MockHttpSession();
                        session.setAttribute(USER_KEY, "foo");
                    HttpServletRequest req =
Mockito.mock(HttpServletRequest.class);
                    when(req.getSession()).thenReturn(session);
                        
                        String responseMsg =
webResource.path("userauth/getuserid").get(String.class);
                        System.out.println(responseMsg);
                        assertTrue(responseMsg != null);
                        
                        try {

                        } catch (Exception err) {
                                fail(err.getMessage());
                        }
                } catch (Exception err) {
                        fail(err.toString());
                }
                
        }

Thanks,

JC