Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
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



Arun Patidar

View solution in original post

11 Replies

Avatar

Community Advisor

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

Community Advisor

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 2

Yeah, its return NULL, that's why am not able to get any properties.

Avatar

Level 6

@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

Community Advisor

Hi

Can you share your model and test class?



Arun Patidar

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



Arun Patidar

Avatar

Level 5

hi @N_haq - was your issue resolved, i am also getting similar issue