Apache
Home » Documentation » Development

JCR Mocks

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.

Maven Dependency

1
2
3
4
<dependency>
  <groupId>org.apache.sling</groupId>
  <artifactId>org.apache.sling.testing.jcr-mock</artifactId>
</dependency>

See latest version on the downloads page.

Implemented mock features

The mock implementation supports:

The following features are not supported:

Usage

Getting JCR mock objects

The factory class MockJcr allows to instantiate the different mock implementations.

Example:

1
2
3
4
5
// 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.

Mocking queries

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.

Example:

1
2
3
4
5
6
7
8
// 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.

Rev. 1644098 by sseifert on Tue, 9 Dec 2014 16:01:26 +0000
Apache Sling, Sling, Apache, the Apache feather logo, and the Apache Sling project logo are trademarks of The Apache Software Foundation. All other marks mentioned may be trademarks or registered trademarks of their respective owners.