Help with JUnit Test for AEM Sling Model | Community
Skip to main content
Level 2
April 29, 2025
Solved

Help with JUnit Test for AEM Sling Model

I'm trying to write unit tests for an AEM Sling Model, but my test is failing. Here's the test I tried:

@RunWith(MockitoJUnitRunner.class)
public class ArticleContentModelTest {

@Rule
public AemContext context = new AemContext();

private ArticleContentModel articleContentModel;

@Before
public void setUp() {
context.create().resource("/content/article", "title", "Sample Article", "body", "This is a sample article body.");
articleContentModel = context.request().adaptTo(ArticleContentModel.class);
}

@2785667
public void testGetTitle() {
assertEquals("Sample Article", articleContentModel.getTitle());
}

@2785667
public void testGetBody() {
assertEquals("This is a sample article body.", articleContentModel.getBody());
}
}

 

I am getting a NullPointerException when trying to assert the title and body in the tests. I have checked that the resource exists, and the ArticleContentModel should have been adapted correctly, but something is wrong.

SantoshSai
SantoshSaiAccepted solution
New Participant
April 29, 2025

Hi @aadhirara,

I think the problem is that your Sling model wasn’t properly registered in the test context. Even if the resource is set properly - the adaptation will silently fail.

Try this: 

context.addModelsForClasses(ArticleContentModel.class); // Registers the Sling model Resource resource = context.resourceResolver().getResource("/content/article"); articleContentModel = resource.adaptTo(ArticleContentModel.class); // Resource-based adaptation

 Hope that help!

Santosh Sai

4 replies

SantoshSai
SantoshSaiAccepted solution
New Participant
April 29, 2025

Hi @aadhirara,

I think the problem is that your Sling model wasn’t properly registered in the test context. Even if the resource is set properly - the adaptation will silently fail.

Try this: 

context.addModelsForClasses(ArticleContentModel.class); // Registers the Sling model Resource resource = context.resourceResolver().getResource("/content/article"); articleContentModel = resource.adaptTo(ArticleContentModel.class); // Resource-based adaptation

 Hope that help!

Translate
Santosh Sai
AadhiraRaAuthor
Level 2
April 29, 2025

Worked. Thanks!

Translate
muskaanchandwani
Employee
April 29, 2025

Hello @aadhirara 

Problem seems to be here :

articleContentModel = context.request().adaptTo(ArticleContentModel.class);

To fix it, make sure the request is pointing to the correct resource(setResource) before calling adaptTo.

@Before public void setUp() { context.create().resource("/content/article", "title", "Test", "body", "test body"); Resource resource = context.resourceResolver().getResource("/content/article"); context.request().setResource(resource); articleContentModel = context.request().adaptTo(ArticleContentModel.class); }

Reference :
https://sling.apache.org/documentation/development/sling-mock.html#slinghttpservletrequest-1

Translate
AadhiraRaAuthor
Level 2
April 29, 2025

@muskaanchandwani Thanks for your input! I actually tried the exact approach you suggested - setting the resource explicitly on the request as you mentioned above. Unfortunately, this still results in articleContentModel being null, and the test fails with a NullPointerException.

Translate