【问题标题】:"resource.adaptTo" NullPointer Unit test AEM Sling Model Java“resource.adaptTo” NullPointer 单元测试 AEM Sling 模型 Java
【发布时间】:2020-11-17 00:49:41
【问题描述】:

我正在 AEM 中为 Sling Model 进行非常基本的单元测试,因此,当我运行测试时,我收到以下错误:

[错误] CtaModelTest.testGetText:36 NullPointer

这是我的 Java 代码,模型是一个非常基本的 Sling AEM 模型,我使用的是@ModelAnnotation,如下所示:

@Model(adaptables = SlingHttpServletRequest.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
package com.myproject.core.models;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.resource.Resource;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import com.day.cq.wcm.api.Page;
import io.wcm.testing.mock.aem.junit5.AemContext;
import io.wcm.testing.mock.aem.junit5.AemContextExtension;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import junitx.util.PrivateAccessor;
import javax.inject.Inject;

@ExtendWith(AemContextExtension.class)
class CtaModelTest {
    private static final String COMPONENT_PATH = "/content/campaigns/myproject/master/demo-campaign/demo-email";
    
    private CtaModel ctaModel;

    private Page page;
    private Resource resource;

    @BeforeEach
    public void setup(AemContext context) throws Exception {
        
        context.load().json("/ctaModelTest.json", COMPONENT_PATH);
        context.addModelsForClasses(CtaModel.class);
        
        resource = context.resourceResolver().getResource(COMPONENT_PATH + "/jcr:content/par/hero/cta");
        ctaModel = resource.adaptTo(CtaModel.class);
    }

    @Test
    void testGetText() throws Exception {
        String txt = ctaModel.getText();
        assertNotNull(txt);
    }
}

谁能帮我解决它?

【问题讨论】:

  • 您能否发布您的模型代码以及您正在加载到测试 AEM 上下文中的 JSON 文档?这可能是一个简单的属性名称不匹配,但如果没有看到 NPE 发生的行就很难说。

标签: java junit aem sling


【解决方案1】:

resource.adaptTo(CtaModel.class) 似乎返回了 null。问题是,如果出现任何故障,adapTo(...) 会非常安静地返回 null。因此,SlingMocks 文档建议 SlingModels 使用 ModelFactory.createModel(...) 而不是 adaptTo(...)。

https://sling.apache.org/documentation/development/sling-mock.html#model-instantiation

// load some content into the mocked repo
context.load().json(..., "/resource1");

// load resource
Resource myResource = content.resourceResolver().getResource("/resource1");

// instantiate Sling Model (adaptable via Resource)
// this will throw exceptions if model cannot be instantiated
MyModel myModel = context.getService(ModelFactory.class).createModel(myResource, MyModel.class);

如果您这样做,ModelFactory 将记录错误详细信息,即无法创建 Sling 模型的原因。所以你知道问题是什么,而且你不需要猜测。

【讨论】:

    【解决方案2】:

    如果 init 方法存在问题,特别是如果注入的任何对象(如 currentPage 或 services)为 null,则即使使用 createModel,model 也会静默返回 null。

    https://myaemlearnings.blogspot.com/2020/08/unit-testing-in-aem-debugging-issues-in.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-10
      • 2022-11-19
      • 2019-04-11
      • 2018-03-13
      • 2016-11-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多