Expand my Community achievements bar.

Submissions are now open for the 2026 Adobe Experience Maker Awards.
SOLVED

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.

1 Accepted Solution

Avatar

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

View solution in original post

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

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