Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

Resource is returning Null from Sling Model class in JUnit

Avatar

Level 2

I have added details of the issue on below link. 

https://stackoverflow.com/questions/67414080/resource-is-returning-null-from-sling-model-class-in-ju...

 

Can you please let me know if I am missing anything.

 

 

1 Accepted Solution

Avatar

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

Avatar

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.

Avatar

Community Advisor

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.

 

Avatar

Correct answer by
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)

Avatar

Community Advisor

Could you please share test json as well?



Arun Patidar

Avatar

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

Avatar

Community Advisor

Hi,

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

 

/** The accoridonArray node */

@inject

@Deleted Account

Resource componentResource;

 

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



Arun Patidar

Avatar

Level 2

Hi @arunpatidar, @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"

 

Avatar

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.