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.
Vijayalakshmi_S
Vijayalakshmi_SAccepted solution
Level 10
Jun 1, 2021 5:52 PM UTC

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)

9 replies

Vijayalakshmi_S
Level 10
Jun 3, 2021 2:16 PM UTC

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.

Translate
Level 2
Jun 2, 2021 5:21 AM UTC

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

 

 

 

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

 

Translate
arunpatidar
New Participant
Jun 1, 2021 6:15 PM UTC

Could you please share test json as well?

Translate
Arun Patidar
Level 2
Jun 2, 2021 12:31 PM UTC
@arunpatidar, FYI I have added Mock JSON and Component Node structure above in the same chain
Translate
Vijayalakshmi_S
Vijayalakshmi_SAccepted solution
Level 10
Jun 1, 2021 5:52 PM UTC

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)
Translate
Prince_Shivhare
New Participant
Jun 1, 2021 11:12 AM UTC

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.

 

Translate
shelly-goel
Employee
Jun 1, 2021 8:04 AM UTC

You can go through this tutorial to assert for Arrays

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

Translate
Level 2
Jun 1, 2021 8:28 AM UTC
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.
Translate