Resource is returning Null from Sling Model class in JUnit | Community
Skip to main content
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 Vijayalakshmi_S

Hi @ks_hitijingole,

From the log statement, resource named "componentResource" that you are injecting in the model is null. (which is the starting point in the getFaqQuestionAnswerArray() method) because of this it a NPE on getFaqQuestionAnswerArray() method from the test class.

Could you please share your

  • Mock JSON file(pdp.json) and
  • Screenshot of component node structure from CRXDE as available under content path (faqcomponent available under the respective /content path say /content/xyz/page/jcr:content/root/responsivegrid/faqcomponent)

6 replies

shelly-goel
Adobe Employee
Adobe Employee
June 1, 2021

You can go through this tutorial to assert for Arrays

https://www.youtube.com/watch?v=g5x6F8bUHj8

Level 2
June 1, 2021
Hi @shelly-goel I have created the Junit test case on the same lines as mentioned in the above tutorial. But the resource is somehow returning null.
Prince_Shivhare
Community Advisor
Community Advisor
June 1, 2021

can you please re-check whatever you are configuring on the dialog is configured on the JSON file?

there should not be any variable miss inside the json.

 

Vijayalakshmi_S
Vijayalakshmi_SAccepted solution
Level 10
June 1, 2021

Hi @ks_hitijingole,

From the log statement, resource named "componentResource" that you are injecting in the model is null. (which is the starting point in the getFaqQuestionAnswerArray() method) because of this it a NPE on getFaqQuestionAnswerArray() method from the test class.

Could you please share your

  • Mock JSON file(pdp.json) and
  • Screenshot of component node structure from CRXDE as available under content path (faqcomponent available under the respective /content path say /content/xyz/page/jcr:content/root/responsivegrid/faqcomponent)
arunpatidar
Community Advisor
Community Advisor
June 1, 2021

Could you please share test json as well?

Arun Patidar
Level 2
June 2, 2021
@arunpatidar, FYI I have added Mock JSON and Component Node structure above in the same chain
Level 2
June 2, 2021

Hi @arunpatidar, @vijayalakshmi_s sharing the Mock Json and component node structure below.

 

 

 

PS : There is a known typo in Node Name "accoridonArray"

 

Vijayalakshmi_S
Level 10
June 3, 2021

Hi @ks_hitijingole,

Per the node structure and mock JSON, could see that componentResource is not available in your mock JSON as said earlier.

Also, looks like you are using multifield to author the questions/answers part of accordion where accordionArray is the multifield node.

In order to retrieve the same, you can use Injector Specific Annotation named @ChildResource (org.apache.sling.models.annotations.injectorspecific.ChildResource)

@ChildResource
private List<Resource> accordionArray;

 

Revise the Sling Model accordingly and for framing mock JSON you can access the path using infinity.json and use that in the JSON file. 

Example :

In this case per the code snippet you shared,

If this path - /content/<PATH>/us/en/home/jcr:content/faq (faq node) has the sling:resourceType to be "roche/diagnostics/components/faq", then

  • access http://localhost:4502/content/<PATH>/us/en/home/jcr:content/faq.infinity.json and use that as is in pdp.json
  • load it to same path /content/<PATH>/us/en/home/jcr:content/faq
    • MockRocheContent.loadfile(context, "/json/<PATH>/us/pages/pdp.json", "/content/<PATH>/us/en/home/jcr:content/faq");
  • use the same path to get Resource object from aemContext -> adapt to FaqModel 
    • modelObject = context.resourceResolver().getResource("/content/<PATH>/us/en/home/jcr:content/faq").adaptTo(FaqModel.class);

If you still face any issues, please update this thread with complete details of the component.