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

Adobe Summit 2023 [19th to 23rd March, Las Vegas and Virtual] | Complete AEM Session & Lab list
SOLVED

Junit test case return NULL error at the time of adapt resource on sling model based on delegation pattern

Avatar

Level 2

Hi All,

 

I am facing NullPointerException on ButtonModelTest. ButtonModel class is created based on delegation pattern, which implements Button(core component class).

 

1 .Button Model class

N_haq_0-1668069337615.png

 

2. ButtonModelTest

 

N_haq_1-1668069488138.png

 

I am facing error at line 45, at the time of get the properties, even my json file is correct. 

 

Please help me find the solutions for junit test case for delegation pattern model classes.

 

Thanks in Advance.

 

@lukasz-m  

 

@arunpatidar 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

You have to follow this to make it work
https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/junit-test-cases-for-sling...

 

I cannot see the more than one adaptables in your model code, thats why I asked if you are following above or not

View solution in original post

1 Reply

Avatar

Level 4

Most of the cases of NullPointerException occur for the bad resource. In your test class check line no 39, you are getting the correct resource.

Avatar

Level 4

Actually, it's not about NULL. To see if the resource is correct or not check the next line. buttonModel is null or not.

Avatar

Level 4

@N_haq 

Did you try adapting model with SlingHttpServletRequest?

buttonModel = context.request().adapt(ButtonModel.class);

 

 

Avatar

Level 2

@arunpatidar  Tried this approach but its not works for me. Still NullPointerException  at time of get the property of Button core class.

Is there any other way to do junit on sling delegation pattern?

 

Thanks. 

Avatar

Correct answer by
Community Advisor

You have to follow this to make it work
https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/junit-test-cases-for-sling...

 

I cannot see the more than one adaptables in your model code, thats why I asked if you are following above or not

The ultimate experience is back.

Join us in Vegas to build skills, learn from the world's top brands, and be inspired.

Register Now