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

Setting up for Magnolia testing

There are some handy classes available when creating tests for your Magnolia functionality. To get them working in your Maven project, add the following dependencies to your project descriptor.

Code Block
titlepom.xml

    ...

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.4</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.mockito</groupId>
      <artifactId>mockito-core</artifactId>
      <version>1.8.5</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>info.magnolia</groupId>
      <artifactId>magnolia-core</artifactId>
      <version>${magnoliaVersion}</version>
      <type>test-jar</type>
      <scope>test</scope>
    </dependency>

    ...

Magnolia Mocking overview

For testing basic functionality that does depend on itself there is no need to use mock objects we can just test that method functionality straight away, but usually we need to test objects that depend on other objects and there is where we use this mock objects, beware that they can just return what you want but the code behind won't be executed.

...

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

MockUtil.createAndSetHierarchyManager(ContentRepository.USERS, getClass().getResourceAsStream("sample.properties"));

...

Code Block
titleCreateMockContentFromString.java

public final String CONTENT = ""
+ "main/content@type = mgnl:contentNode"
+ "main/content@uuid = 4"
+ "main/content.value = Content Value";

MockUtil.createAndSetHierarchyManager(ContentRepository.USERS, getClass().getResourceAsStream("sample.properties"));

...

Code Block
titleCreateMockContentUsingAPI.java

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

...