Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

Mocked Accordion Object returning null in CustomImpl Class - JUnit

Avatar

Community Advisor

Hi AEM Community,

I have a strange issue when writing a JUnit for my Test class.

This is my custom impl class's code snippet which is being tested -

 

1. There is an object of Accordion interface

Rohan_Garg_0-1667368585301.png

 

2. We are creating a custom getData method to create a custom analytics layer. As seen below a resource object is created from SlingHttpServletRequest adaptable interface.

The accordion object is used to inherit features from core component's data layer while resource object is used to get JCR data.

Rohan_Garg_1-1667368664256.png

 

This is working fine when being called upon by the Accordion component in a page.

 

However, when running the same method via Test class as shown below - the accordion object is being returned as null even though it has been mocked!

Rohan_Garg_2-1667368811489.png

 

The mocked accordion object is being created successfully in test class but renders itself as null in impl class.

Is there something amiss with mocking the below object ?

Rohan_Garg_3-1667371574679.png  Rohan_Garg_4-1667371594614.png

 

@arunpatidar, @kautuk_sahni, @Mohit_KBansal, @B_Sravan, @Vijayalakshmi_S 

 

Thanks,

Rohan Garg

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@arunpatidar  - Thanks for the link. My scenario differs slightly in below way -

 

I have 1 custom interface CustomAccordion which is not extending Accordion.

Rohan_Garg_0-1667383626758.png

 

The implementation class for this is using the delegation pattern.

Rohan_Garg_0-1667383717102.png

 

Observations

1. defaultInjectionStrategy=DefaultInjectionStrategy.OPTIONAL is already present

2. Use one more adaptable to adapt Core Accordion Component not working. Its gives build error.

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

@arunpatidar  - Thanks for the link. My scenario differs slightly in below way -

 

I have 1 custom interface CustomAccordion which is not extending Accordion.

Rohan_Garg_0-1667383626758.png

 

The implementation class for this is using the delegation pattern.

Rohan_Garg_0-1667383717102.png

 

Observations

1. defaultInjectionStrategy=DefaultInjectionStrategy.OPTIONAL is already present

2. Use one more adaptable to adapt Core Accordion Component not working. Its gives build error.