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

sample.properties
page1.@type = mgnl:content
page1/page11.@type = mgnl:content
page1/page12.@type = mgnl:content
page1/page13.@type = mgnl:content
page1/collection.@type = mgnl:contentNode
page1/collection/para11.@type = mgnl:contentNode
page1/collection/para12.@type = mgnl:contentNode
page1/page12/collection/para121.@type = mgnl:contentNode
page1/page12/collection/para122.@type = mgnl:contentNode
create MockSession from properties file
MockSession session = SessionTestUtil.createSession("test", getClass().getResourceAsStream("sample.properties"));

as well as from String:

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

Of course there's also an proper API for it:

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

Setup

In order to use these testing utilities, you must add the following dependency to your project:

<dependency>
    <groupId>info.magnolia</groupId>
    <artifactId>magnolia-core</artifactId>
    <type>test-jar</type>
    <scope>test</scope>
    <version>${magnolia.version}</version>
</dependency>


  • No labels

7 Comments

  1. Each of these code snippets have file names. Im confused, are there actual files somewhere with these names? Should they rather be titles with separated words?

    1. +1 - I think at some point the examples reflected real files. Right now it's just confusing so I went along and "fixed" it

  2. Is still possible to create a JCR Session from a property file? Like Content API Mocks?

    Does Magnolia 5.1+ offer any additional support?

  3. The first example on this page shows how to create a (mock) JCR Session from property file. Maybe the title was confusing - I changed it to "create MockSession from properties file". For 5.1+ there's no plans yet to add more convenience - are you missing something in particular?

  4. This is fine in order to test JCR Session. 

    Since Magnolia has a lot of additiona features (workflow, activation framework, ACLs, templating framework..), it would be nice to have something that let me test other parts. I was referring to this.

    E.g.: a Selenium test that fill content using Magnolia dialogs and look in a configured page if that content is fine or not (according to some rule..)....

  5. I see - so there's indeed a growing number of SeleniumUI tests. See Wiki for further infos.

  6. It should be noted that the normal mangolia-core module jar does not include these testing utils but that you have to add this dependency to get them:

    <dependency>
    <groupId>info.magnolia</groupId>
    <artifactId>magnolia-core</artifactId>
    <type>test-jar</type>
    <scope>test</scope>
    <version>${magnolia.version}</version>
    </dependency>