Thursday, February 07, 2013

Tip: Hibernate PersistenceException :

If you are seeing the following error "No Persistence provider for EntityManager named" and scratching your brains, ensure that the hibernate-entitymanager.jar is on the classpath.

Also you should have a META-INF/persistence.xml in your jars someplace.

Monday, February 04, 2013

Tuesday, September 11, 2012

Mockito return the value from the response

An example:
private HttpServletRequest request = mock(HttpServletRequest.class);
    private HttpServletResponse response = mock(HttpServletResponse.class);

    @Test
    public void testAuthz() throws Exception {
        AuthorizationEndpoint endpoint = new AuthorizationEndpoint();
        when(request.getParameter("response_type")).thenReturn(ResponseType.CODE.toString());
        when(request.getParameter("client_id")).thenReturn("test_id");
        when(request.getParameter("redirect_uri")).thenReturn("http://localhost");

        final ArrayList values = new ArrayList();
        doAnswer(new Answer() {
            @Override
            public Object answer(InvocationOnMock invocation) throws Throwable {
                String temp = (String) invocation.getArguments()[0];
                values.add(temp);
                return null;
            }
        }).when(response).sendRedirect(anyString());

        httpRequest("GET", "/authorization");

        endpoint.service(request, response);

        String redirectURL = values.get(0);

        URL url = new URL(redirectURL);
        String fragment = url.toURI().getQuery();
        Map map = OAuthUtils.decodeForm(fragment);

        assertNotNull(map.get(OAuth.OAUTH_CODE));
    }

    private void httpRequest(String method, String pathInfo) {
        when(request.getMethod()).thenReturn(method);
        when(request.getPathInfo()).thenReturn(pathInfo);
    }