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

Compare with Current View Page History

« Previous Version 7 Next »


Magnolia uses JUnit tests and EasyMock for creating unit tests.
Information about EasyMock can be found www.easymock.org

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.

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:

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

By doing this you can access to the nodes of you 'fake' repositoy as if it where real. But if you really need to use a real repository you can extend from RepositoryTestCase and use the methods declared in it.

Useful classes included in magnolia-core for building tests:

  • MgnlTestCase - Sets up a basic environment for the test, loads beans and modules properties and initializes a mock context as the local context.
  • MockUtil - You can create mock objects and using createHierarchyManager you can build mock content based on a property file. The content can be force to be ordered and it allows you to create nodes, content, properties...
  • MockContent - emulates a Content object used by MockUtil
  • RepositoryTestCase - Can be used to test on a real repository. It will initialize and delete it when finished.
  • FactoryUtil - Class to allow various kinds of classes instantiations. Includes methods to convert content to beans.
final SystemContext sysCtx = createStrictMock(SystemContext.class);

        sysCtx.setLocale(Locale.ENGLISH);

        FactoryUtil.setInstance(SystemContext.class, sysCtx);

What this example does is to create a strict mock of the SystemContext Class that is it creates a mock object that implements this interface with order checking. 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()

  • No labels