Skip to content

Instantly share code, notes, and snippets.

@thecarlhall
Created March 6, 2012 22:13
Show Gist options
  • Save thecarlhall/1989349 to your computer and use it in GitHub Desktop.
Save thecarlhall/1989349 to your computer and use it in GitHub Desktop.
Unit Test Mocks for JCR, Sparse Sessions
private javax.jcr.Session jcrSession;
private org.sakaiproject.nakamura.api.lite.Repository repository;
private org.sakaiproject.nakamura.api.lite.Session session;
// Create an in-memory repository to work with
repository = new BaseMemoryRepository().getRepository();
// mock a jcr session but also include SessionAdaptable for easier mocking to a sparse session
jcrSession = mock(javax.jcr.Session.class, Mockito.withSettings().extraInterfaces(SessionAdaptable.class));
// mock the session to return the sparse session
repository.loginAdministrative().getAuthorizableManager().createUser("someUser");
session = repository.loginAdministrative("someUser");
Mockito.when(((SessionAdaptable) jcrSession).getSession()).thenReturn(session);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment