Versions Compared

Key

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

...

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:

...

Imagine you need to test something that depends on a repository, well you can 'mock' a repository and it's content without having to create the repository itself. You can extend your test class from MgnlTestCase that will setup the basic environment for you and then use something like:

Code Block

HierarchyManager hm = MockUtil.createAndSetHierarchyManager(REPOSITORY, CONTENT);

...

  • FactoryUtil - Class to allow various kinds of classes instantiations. Includes methods to convert content to beans.
Code Block

final SystemContext sysCtx = createStrictMock(SystemContext.class);

        sysCtx.setLocale(Locale.ENGLISH);

        FactoryUtil.setInstance(SystemContext.class, sysCtx);

What this example does is to use the EasyMock method createStrictMock that creates an instance of the SystemContext inteface meaning that it creates a mock object that implements this interface checking the order of method calls. It sets the property Locale to this new context and then uses the FactoryUtil class to register the new instance which will be returned by getSingleton()

Magnolia Mock Objects

Content API Mocks

...

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);

...

Code Block
titleCreateMockContentUsingAPI.java

MockContent page = new MockContent("page");
page.createContent("subpage", ItemType.CONTENT);
page.setNodeData("stringProperty", "HelloWorld");

...

Code Block
titleCreateMockNodeFromPropertiesFile.java

MockSession session = SessionTestUtil.createSession("test", getClass().getResourceAsStream("sample.properties"));

...

Code Block
titleCreateMockContentUsingAPI.java

MockSession session = SessionTestUtil.createSession("testWorkspace",
    "/foo/bar.@type=mgnl:content",
    "/foo/bar/sub1.@uuid=1",
    "/foo/bar/subpath.property=testName");

...

Code Block
titleCreateMockNodeUsingAPI.java

MockNode root = new MockNode();
root.addNode(MetaData.DEFAULT_META_NODE);
root.setProperty("stringProperty", "HelloWorld");