Expand my Community achievements bar.

Adobe Summit 2025: AEM Session Recordings Are Live! Missed a session or want to revisit your favorites? Watch the latest recordings now.

Getting Null pointer exception while mocking resourceResolver

Avatar

Level 1

In my SImple.java class inside workflow

I am having 

try(ResourceResolver resourceResolver = getResolver(resourceResolverFactory)) {.......}

 

In my SImpleTest.java class

Resource resource = context.resourceResolver().getResource("/content/........");

cu.excute(workItem, workflowSession, processArguments);

 

 

getting null pointer exception test class for cu.excute method.

Any other approach to handle resourceResolverFactory ? 

Topics

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

3 Replies

Avatar

Employee

@dhananjayKu5 you can try to mock ResourceResolverFactory and inject a working ResourceResolver in your test using AEM Mocks :
https://experienceleague.adobe.com/en/docs/experience-manager-learn/getting-started-wknd-tutorial-de...

Avatar

Community Advisor

Hi @dhananjayKu5 ,
Did you try using mockito to mock the required things: import org.mockito.Mockito;

ResourceResolverFactory resourceResolverFactory = mock(ResourceResolverFactory.class);
ResourceResolver resourceResolver = mock(ResourceResolver.class);
Resource resource = mock(Resource.class);

Avatar

Community Advisor and Adobe Champion

Do not mock the Resource Resolver yourself; the mocks from AEM Mocks work great.

Use org.apache.sling.testing.resourceresolver.MockResourceResolverFactory.

 

Good luck,

Daniel