JUnit - Page model NullPointerException | Community
Skip to main content
Nitin_laad
New Participant
Nov 25, 2020 8:29 PM UTC
Solved

JUnit - Page model NullPointerException

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 please.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
BrianKasingli
BrianKasingliAccepted solution
New Participant
Nov 26, 2020 10:26 PM UTC

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.

10 replies

BrianKasingli
BrianKasingliAccepted solution
New Participant
Nov 26, 2020 10:26 PM UTC

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.

Translate
Nitin_laad
New Participant
Nov 27, 2020 3:35 PM UTC
I am able to successfully run below test case
Translate
BrianKasingli
New Participant
Nov 26, 2020 5:03 PM UTC

@nitin_laad 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); } }

 

 

Translate
Nitin_laad
New Participant
Nov 26, 2020 8:55 PM UTC
Thank You @briankasingli, tried with suggested option but no luck
Translate
Vijayalakshmi_S
Level 10
Nov 26, 2020 4:20 PM UTC

Hi @nitin_laad,

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.

Translate
Nitin_laad
New Participant
Nov 26, 2020 8:51 PM UTC

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

Getting NullPointer for pageModel.getTitle(). 

Translate
Level 4
Nov 26, 2020 4:40 AM UTC

Hi @nitin_laad ,

Please try below line.

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

https://experienceleague.adobe.com/docs/experience-manager-learn/getting-started-wknd-tutorial-develop/unit-testing.html?lang=en#testing-get-occupations 

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

Thanks,

Sandeep.

 

Translate
Nitin_laad
New Participant
Nov 26, 2020 8:57 PM UTC

Thank You @sandeep6, pfa json

Translate