Resource is returning Null from Sling Model class in JUnit

Avatar

Avatar
Ignite 1
Level 2
ks_hitijingole
Level 2

Likes

3 likes

Total Posts

21 posts

Correct reply

0 solutions
Top badges earned
Ignite 1
Give Back 3
Give Back
Boost 3
Boost 1
View profile

Avatar
Ignite 1
Level 2
ks_hitijingole
Level 2

Likes

3 likes

Total Posts

21 posts

Correct reply

0 solutions
Top badges earned
Ignite 1
Give Back 3
Give Back
Boost 3
Boost 1
View profile
ks_hitijingole
Level 2

31-05-2021

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.

 

 

ks_hitijingole

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

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.

View Entire Topic

Avatar

Avatar
Coach
MVP
Arun_Patidar
MVP

Likes

1,444 likes

Total Posts

3,327 posts

Correct reply

941 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile

Avatar
Coach
MVP
Arun_Patidar
MVP

Likes

1,444 likes

Total Posts

3,327 posts

Correct reply

941 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile
Arun_Patidar
MVP

01-06-2021

Could you please share test json as well?