Expand my Community achievements bar.

SOLVED

Unit test case NPE

Avatar

Level 2

Hi everyone,

I’m trying to write unit tests for a custom Sling Model for my PresentLine component, but I’m running into issues where the model always adapts to null during the test — even though the component works completely fine in AEM.

Here’s my component’s Sling Model:

PresentLineModel.java

package com.myproject.core.models;

import javax.inject.Inject;

import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;

@Model(
adaptables = Resource.class,
defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL
)
public class PresentLineModel {

@inject
private String lineText;

@inject
private String lineStyle;

public String getFormattedLine() {
if (lineText == null) return "";
return lineStyle != null && lineStyle.equals("uppercase")
? lineText.toUpperCase()
: lineText;
}
}

 
 

Now here is the unit test:

PresentLineModelTest.java

package com.myproject.core.models;

import static org.junit.jupiter.api.Assertions.*;

import org.apache.sling.testing.mock.sling.junit5.AemContext;
import org.apache.sling.testing.mock.sling.junit5.AemContextExtension;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

@ExtendWith(AemContextExtension.class)
class PresentLineModelTest {

private final AemContext context = new AemContext();

@test
void testFormattedLine() {

context.addModelsForClasses(PresentLineModel.class);

context.create().resource(
"/content/test/presentline",
"sling:resourceType", "myproject/components/presentline",
"lineText", "hello world",
"lineStyle", "uppercase"
);

var resource = context.resourceResolver().getResource("/content/test/presentline");

PresentLineModel model = resource.adaptTo(PresentLineModel.class); -> null

assertNotNull(model, "Model should not be null");
assertEquals("HELLO WORLD", model.getFormattedLine());
}
}

No matter what I try, resource.adaptTo(PresentLineModel.class) always returns null.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @OmarKh3,

 Try this: 

@Test
void testFormattedLine() {

    context.registerInjectActivateService(new org.apache.sling.models.impl.ModelAdapterFactory());
    context.addModelsForClasses(PresentLineModel.class);

    context.create().resource(
            "/content/test/presentline",
            "sling:resourceType", "myproject/components/presentline",
            "lineText", "hello world",
            "lineStyle", "uppercase"
    );

    var resource = context.resourceResolver().getResource("/content/test/presentline");

    PresentLineModel model = resource.adaptTo(PresentLineModel.class);

    assertNotNull(model);
    assertEquals("HELLO WORLD", model.getFormattedLine());
}

Santosh Sai

AEM BlogsLinkedIn


View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

Hi @OmarKh3,

 Try this: 

@Test
void testFormattedLine() {

    context.registerInjectActivateService(new org.apache.sling.models.impl.ModelAdapterFactory());
    context.addModelsForClasses(PresentLineModel.class);

    context.create().resource(
            "/content/test/presentline",
            "sling:resourceType", "myproject/components/presentline",
            "lineText", "hello world",
            "lineStyle", "uppercase"
    );

    var resource = context.resourceResolver().getResource("/content/test/presentline");

    PresentLineModel model = resource.adaptTo(PresentLineModel.class);

    assertNotNull(model);
    assertEquals("HELLO WORLD", model.getFormattedLine());
}

Santosh Sai

AEM BlogsLinkedIn


Avatar

Level 2

@SantoshSai This worked, thank you!