I'm doing a pretty basic Unit test for Sling Model in AEM, so, when I run the test I get the following error:
[ERROR] CtaModelTest.testGetText:36 NullPointer
This is my Java code, the Model is a very basic Sling AEM Model, I'm using the @ModelAnnotation as follow:
@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);
}
}
Can anyone help me to fix it?