Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

JUnit - Page model NullPointerException

nitinjl
Level 4
Level 4

Hi All, 

Trying to get Page model object (OOB) in JUnit test case, but getting NullPointerException (at line#75).
Am I missing anything in the implementation, any thoughts/suggestion pleaseNullPointer.png.

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution
BrianKasingli
Correct answer by
Community Advisor
Community Advisor

Try again after making your Sling Model more simple. Change to :

@Model(adaptables = {
          SlingHttpServletRequest.class, 
          Resource.class
     }, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class PageModel implements Page {

}

 If this doesn't work, try to remove implements Page. Just seeing it your developer's environment is working at all. It might just be a coding convention issue.

View solution in original post

0 Replies
Sandeep6
Level 5
Level 5

Hi @nitinjl ,

Please try below line.

PageModel model = context.request().adaptTo(PageModel.class);

https://experienceleague.adobe.com/docs/experience-manager-learn/getting-started-wknd-tutorial-devel... 

If possible, please share json what you are referencing below. 

Thanks,

Sandeep.

 

nitinjl
Level 4
Level 4
Thanks Sandeep, somehow missed to submit the request, here you go..
Vijayalakshmi_S
Community Advisor
Community Advisor

Hi @nitinjl,

Please check the Model under test. In this case, PageModel.class - If any of the injects/references used in sling model is null in the context of test environment, then it will result in Model being null. 

We need to set those references used in Model to aemContext and then use it to get the Model object. 

Example : If a Model uses Externalizer service, then we need to set the Externalizer to aemContext first before instantiating the Model. 

Please share the PageModel.class if it is fine to share for further debugging.

nitinjl
Level 4
Level 4

Thank You Vijaylaxmi for your time, Here is the model definition....and related junit and json (modified version)model_new.pngmodel_new_json.pngmodeltest_new.png

Getting NullPointer for pageModel.getTitle(). 

BrianKasingli
Community Advisor
Community Advisor

@nitinjl do you have the RunWith annotation set?

 

@RunWith(MockitoJUnitRunner.class)
public class ComponentTest {
    @Rule
    public final AemContext context = new AemContext(ResourceResolverType.JCR_MOCK);

    private Component component;
   
    @Test
    public void itShouldNotBeNull() {
         Resource componentContext = context.create().resource("/content/my-site/home/jcr:content/component", new ValueMapDecorator(ImmutableMap.<String, Object> of(
                "fakeProp", "example")));
        underTest = componentContext.adaptTo(Component.class);
        assertNotNulll(underTest);
    }
}

 

 

BrianKasingli
Correct answer by
Community Advisor
Community Advisor

Try again after making your Sling Model more simple. Change to :

@Model(adaptables = {
          SlingHttpServletRequest.class, 
          Resource.class
     }, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class PageModel implements Page {

}

 If this doesn't work, try to remove implements Page. Just seeing it your developer's environment is working at all. It might just be a coding convention issue.

View solution in original post