Search code examples
mockingadobeaemsling

AEM 6.3, wcm.io: mocking LiveRelationshipManager


I want to test a service that in itself references the LiveRelationshipManager:

@Reference
private LiveRelationshipManager liveRelationshipManager;

The implementation of the LiveRelationshipManager is hidden, I only have the api. How can I register it in my aemContext like for example my own LanaguageService:

aemContext.registerInjectActivateService(new LanguageService());

One solution I found was creating a mock class myself:

@Component(service = LiveRelationshipManager.class)
public class MockLiveRelationshipManager implements LiveRelationshipManager {

But how do I prevent it from being used in my real application and only in my unit tests? Or is there a better way?

Thanks in advance!


Solution

  • If you want to use the mock class only in your tests you should not annotate it with @Component. Just create the mock implementation and then use

    context.registerService(LiveRelationshipManager.class, new MockLiveRelationshipManager())
    

    to add it to your context.