Junit test case return NULL error at the time of adapt resource on sling model based on delegation pattern | Community
Skip to main content
Level 2
November 10, 2022
Solved

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

  • November 10, 2022
  • 3 replies
  • 6387 views

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

 

2. ButtonModelTest

 

 

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 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by arunpatidar

Above screenshots shared model and test classes . 


You have to follow this to make it work
https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/junit-test-cases-for-sling-models-based-on-delegation-pattern/m-p/557666#M138507

 

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

3 replies

Sady_Rifat
Community Advisor
Community Advisor
November 10, 2022

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.

N_haqAuthor
Level 2
November 10, 2022

line39 is correct, its not null.

Sady_Rifat
Community Advisor
Community Advisor
November 10, 2022

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

Community Advisor
November 10, 2022

@n_haq 

Did you try adapting model with SlingHttpServletRequest?

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

 

 

arunpatidar
Community Advisor
Community Advisor
November 10, 2022
N_haqAuthor
Level 2
November 12, 2022

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

N_haqAuthor
Level 2
November 14, 2022

Hi

Can you share your model and test class?


Above screenshots shared model and test classes .