代码之家  ›  专栏  ›  技术社区  ›  Jdruwe

AEM 6.3,wcm。io:mocking LiveRelationshipManager

  •  0
  • Jdruwe  · 技术社区  · 7 年前

    我想测试一个本身引用LiveRelationshipManager的服务:

    @Reference
    private LiveRelationshipManager liveRelationshipManager;
    

    LiveRelationshipManager的实现是隐藏的,我只有api。如何在aemContext中注册它,例如我自己的LanguageService:

    aemContext.registerInjectActivateService(new LanguageService());
    

    我找到的一个解决方案是自己创建一个模拟类:

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

    但是,我如何防止它在我的实际应用程序中使用,并且仅在我的单元测试中使用呢?还是有更好的方法?

    提前感谢!

    1 回复  |  直到 7 年前
        1
  •  1
  •   Daniel Platon    7 年前

    如果你只想在测试中使用mock类,你应该 为其添加注释 @Component . 只需创建模拟实现,然后使用

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

    将其添加到您的上下文中。