Page tree
Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 44 Next »

Your Rating: Results: 1 Star2 Star3 Star4 Star5 Star 0 rates


Magnolia uses JUnit 4 tests and Mockito (new) for creating unit tests. Before we were using EasyMock for dynamic mocks.
Independently from that make sure your tests match our conventions. Information on how to best migrate JUnit3-style tests to JUnit4 can be found here.


Mockito is a more recent mocking library. We'll not bulk convert existing EasyMock-Tests as this would be to big an effort. Instead we set up the following rules:

  • all new tests requiring dynamic mocks use Mockito
  • whenever you touch (fix, adapt, complete) an existing test that's using EasyMock: convert it to Mockito

Magnolia Mock Objects

Content API Mocks

The customized mock objects that we provide to be able to setup an environment for Magnolia tests can be found in package info.magnolia.test.mock.

When creating a mock hierarchy manager you can either initialize it from a properties file:
main@type = mgnl:content
main@uuid = 1

main/uuidLink@type = mgnl:contentNode
main/uuidLink@uuid = 2
main/uuidLink.MetaData.mgnl\:template = someParagraphName
main/uuidLink.MetaData.mgnl\:authorid = superuser
main/uuidLink.MetaData.mgnl\:activatorid = superuser
main/uuidLink.MetaData.mgnl\:title = myTitle
main/uuidLink.link1 = 3

main/linkTarget@type = mgnl:content
main/linkTarget@uuid = 3
main/linkTarget.prop1 = sub2value1
main/linkTarget.prop2 = sub2value2
main/linkTarget.prop3 = boolean:false

main/content@type = mgnl:contentNode
main/content@uuid = 4
main/content.value = Content Value
HierarchyManager hm = MockUtil.createAndSetHierarchyManager(ContentRepository.USERS, getClass().getResourceAsStream(""));

or you can create a variable with the data or pass it to the method call directly:
final String CONTENT = StringUtils.join(Arrays.asList(
    "main/content.value=Content Value"
    ), "\n");

HierarchyManager hm = MockUtil.createAndSetHierarchyManager(ContentRepository.USERS, CONTENT);

You may also directly instantiate a MockContent and then add the child Contents and or NodeData's as required
MockContent page = new MockContent("page");
page.createContent("subpage", ItemType.CONTENT);
page.setNodeData("stringProperty", "HelloWorld");

JCR Mocks

Since Magnolia 4.5 we provide the package info.magnolia.test.mock.jcr containing proper mock's for javax.jcr.Node, javax.jcr.Session etc. They're extending abstract types provided in jackrabbit-commons. The types of the Content API are now basically just wrapping these new mocks. If required you can always create a MockContent from a MockNode or a MockHierarchyManager from a MockSession.

With the help of info.magnolia.test.mock.jcr.SessionTestUtil MockNodes can be created from properties files
MockSession session = SessionTestUtil.createSession("test", getClass().getResourceAsStream(""));

as well as from String:
MockSession session = SessionTestUtil.createSession("testWorkspace",

Of course there's also an proper API for it:
MockNode root = new MockNode();
root.setProperty("stringProperty", "HelloWorld");
  • No labels