[MAGNOLIA-1159] testing: facilitate usage of mocks Created: 25/Oct/06  Updated: 23/Jan/13  Resolved: 25/Oct/06

Status: Closed
Project: Magnolia
Component/s: None
Affects Version/s: 3.0 RC3
Fix Version/s: 3.0 RC4

Type: Improvement Priority: Major
Reporter: Philipp Bärfuss Assignee: Philipp Bärfuss
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Template:
Acceptance criteria:
Empty
Task DoD:
[ ]* Doc/release notes changes? Comment present?
[ ]* Downstream builds green?
[ ]* Solution information and context easily available?
[ ]* Tests
[ ]* FixVersion filled and not yet released
[ ]  Architecture Decision Record (ADR)
Date of First Response:

 Description   

We should allow to use mocks created with EasyMocks in the FactoryUtil.

Currently the FactoryUtil instantiates an object by itslef and one can only define the implementation class (works really fine), but for testing it would be nice if we could register mocks with a method registerMock() and switch them on with setMocksAllowed().

Using this we are able to mock the major parts in magnolia.



 Comments   
Comment by Philipp Bracher [ 25/Oct/06 ]

Thought so

This code here won't work:

ModuleLoader loader = createMock(ModuleLoader.class);
expect(loader.getModuleInstance(LicenseConsts.MODULE_ENTERPRISE)).andReturn(eeModule);
replay(loader);
FactoryUtil.registerMock(ModuleLoader.class, loader);

because createMock(ModuleLoader.class) triggers already the call to FactoryUtil.getSingleton() before we were able to register the mock. This happens because of this line:

private static ModuleLoader instance = (ModuleLoader) FactoryUtil.getSingleton(ModuleLoader.class);

If we use the appoach described above (I think we should, because it is easy to use), we need to lazy bind the instance variables.

Comment by Philipp Bracher [ 25/Oct/06 ]

I did a few things to solve this:

  • the managers do not have an instance variable anymore but getInstance() uses always the FactoryUtil.
  • I added FactoryUtil.setInstance(interf, obj) allowing to set an instance at runtime
  • I added FactoryUtil.setInstanceFactory(interf, factory) to allow providing a factory (if we are not using singletons)
  • I added FactoryUtil.clear()
  • Fall back to the provided class itself if the implementation property is not yet defined.

The following test is now working fine:

ModuleLoader loader = createMock(ModuleLoader.class);
expect(loader.getModuleInstance(LicenseConsts.MODULE_ENTERPRISE)).andReturn(eeModule);
replay(loader);
FactoryUtil.setInstance(ModuleLoader.class, loader);
LicenseManager.getInstance().checkLicense(license); // will use ModuleLoader at some places

Comment by Philipp Bracher [ 25/Oct/06 ]

I did alos rename getInstance() to newInstance (getInstace is deprecated but working)

Comment by Philipp Bracher [ 25/Oct/06 ]

done

Generated at Mon Feb 12 03:24:14 CET 2024 using Jira 9.4.2#940002-sha1:46d1a51de284217efdcb32434eab47a99af2938b.