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
BedrockMission!

Learn more

View all

Sign in to view all badges

SOLVED

Resource is returning Null from Sling Model class in JUnit

1 Accepted Solution
Vijayalakshmi_S
Correct answer by
Community Advisor
Community Advisor

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)

View solution in original post

9 Replies
ks_hitijingole
Level 2
Level 2
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
Level 9
Level 9

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
Correct answer by
Community Advisor
Community Advisor

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)

View solution in original post

Arun_Patidar
Community Advisor
Community Advisor

Could you please share test json as well?

ks_hitijingole
Level 2
Level 2
@Arun_Patidar, FYI I have added Mock JSON and Component Node structure above in the same chain
Arun_Patidar
Community Advisor
Community Advisor

Hi,

There was an issue in your model class., you have to use @Self annotation to inject componentResource

 

/** The accoridonArray node */

@inject

@Self

Resource componentResource;

 

can you try with https://github.com/arunpatidar02/aem63app-repo/tree/master/java/junit/faq

ks_hitijingole
Level 2
Level 2

Hi @Arun_Patidar, @Vijayalakshmi_S sharing the Mock Json and component node structure below.

 

ks_hitijingole_0-1622611315341.png

 

ks_hitijingole_1-1622611440730.png

 

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

 

Vijayalakshmi_S
Community Advisor
Community Advisor

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.