“resource.adaptTo”NullPointer 单元测试 AEM Sling 模型 Java

问题描述 投票:0回答:3

我正在 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);
    }
}

谁能帮我解决这个问题吗?

java junit aem sling
3个回答
3
投票

看起来

resource.adaptTo(CtaModel.class)
返回了null。问题是,如果出现任何问题,adaptTo(...) 会非常安静地返回 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 模型的原因。所以你知道问题是什么,不需要猜测。


0
投票

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

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


0
投票

确保您已在模型配置中定义了 defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL。这很快就解决了我的问题

@Model(
    adaptables = SlingHttpServletRequest.class,
    adapters = {MyModel.class, ComponentExporter.class},
    resourceType = MyModelImpl.RESOURCE_TYPE,
    defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL
)
@Exporter(
    name = ExporterConstants.SLING_MODEL_EXPORTER_NAME,
    extensions = ExporterConstants.SLING_MODEL_EXTENSION
)
public class MyModelnImpl extends AbstractComponentImpl implements MyModel{
© www.soinside.com 2019 - 2024. All rights reserved.