Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  • 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:

Code Block
titlesample.properties
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
Code Block
titleCreateMockContentFromPropertiesFile.java
HierarchyManager hm = MockUtil.createAndSetHierarchyManager(ContentRepository.USERS, getClass().getResourceAsStream("sample.properties"));

or you can create a variable with the data or pass it to the method call directly:

Code Block
titleCreateMockContentFromString.java
final String CONTENT = StringUtils.join(Arrays.asList(
    "main/content@type=mgnl:contentNode",
    "main/content@uuid=4",
    "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

Code Block
titleCreateMockContentUsingAPI.java
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.

...