@Test
public void testAuthenticateOptionalAuth() {

    HttpServletRequest httpServletRequest = Mockito.mock(HttpServletRequest.class);
    HttpServletResponse httpServletResponse = Mockito.mock(HttpServletResponse.class);
    Authorizer authorizer = Mockito.mock(Authorizer.class);
    Http.AuthorityList authorities = new Http.AuthorityList();
    ResourceContext context = new ResourceContext(httpServletRequest, httpServletResponse, authorities, authorizer);
    // with optional auth we should get null response
    assertNull(context.authenticate(true));
    // without optional auth we should get back an exception
    context.checked = false;
    try {
        context.authenticate(false);
        fail();
    } catch (ResourceException ex) {
        assertEquals(ex.getCode(), ResourceException.UNAUTHORIZED);
    }
}