Mock implementation of selected JCR APIs for easier testing. It stores all data in-memory in a HashMap to ensure instantly creating and destroying of the JCR repository.
<dependency> <groupId>org.apache.sling</groupId> <artifactId>org.apache.sling.testing.jcr-mock</artifactId> </dependency>
See latest version on the downloads page.
The mock implementation supports:
The following features are not supported:
The factory class
MockJcr allows to instantiate the different mock implementations.
// get session Session session = MockJcr.newSession(); // get repository Repository repository = MockJcr.newRepository();
The repository is empty and contains only the root node. You can use the JCR API to read or write content.
If you want to test code that contains a JCR query you can simulate a query execution and set the result to return during setting up your unit test.
// prepare mocked search result List<Node> resultNodes = ImmutableList.of(node1, node2, node3); // return this result for all queries MockJcr.setQueryResult(session, resultNodes); // return this result for a specific query MockJcr.setQueryResult(session, "your query statement", Query.JCR_SQL2, resultNodes);
Alternatively you can use the
MockJcr.addQueryResultHandler method to pass a callback object that allows you to return a query result after inspecting the given query object.