Expand my Community achievements bar.

resourceResolverFactory getting null in serviceImpl class while writing Junit.

Avatar

Level 5

Hi team,

 we are on AEMCS ENV, we were trying to write test case for a search servlet. when we execute the search servlet doget method from servlet its executing successfully but in SearchserviceImpl.java Class the @Mock private ResourceResolverFactory resourceResolverFactory getting null. Kindly please suggest on this. please find the code snippet.

rajat168_0-1725025119559.png

In Junit TestCase file the factory was not null ,PFB screenshot.

rajat168_1-1725025236404.png

rajat168_2-1725025337969.png

 

In SearchServiceImpl, its getting null. PFB screenshot.

rajat168_3-1725025408865.png

 

my searchresultServlet, PFB

rajat168_4-1725025534709.png

 

 

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

2 Replies

Avatar

Level 8

Hi @rajat168 ,

You may try below code for AEM context instead of JCR_MOCK

  private AemContext context = new AemContext(ResourceResolverType.RESOURCERESOLVER_MOCK);

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/junit5-and-mockito-tests-n...

 

Thanks

Avatar

Level 9

You are already using AEM Mocks, so there are many mock objects/statements which you don't really need (session, page, request, response, page etc.)
Most of these you can just get from the AEM context. For e.g you can remove the mock request and mock response and instead can directly rely on context.request() and context.response()
Try calling your doGet with these like
searchResultServlet.doGet(context.request(), context.response())
and move forward from there.
Hope this helps.