Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Creating Unit Test Cases for AEM Servlet with "system user" using Mockito

Avatar

Level 3

Hi experts, 

My code works to create unit test cases for AEM servlet where session is being taken through request as shown below.

Session session = req.getResourceResolver().adaptTo(Session.class);

Below test code works fine:

public final AemContext ctx = new AemContext(ResourceResolverType.JCR_MOCK);

ctx.request().setAttribute(SlingBindings.class.getName(), slingBindings);

mycustomclass.doGet(ctx.request(), ctx.response());

 

I need to know where resourceResolver is retrieved through ResourceResolverFactory and a "system" user. 

Map<String, Object> authentication = Collections.singletonMap("sling.service.subservice", (Object) systemUserName);
resourceResolver = resolverFactory.getServiceResourceResolver(authentication);

 

I get nullpointerexception at the above line. May be I need to manipulate resolverFactory object through AEMContext before calling doGet().

Can anyone please help?

 

Thanks,

Parvathy

1 Accepted Solution

Avatar

Correct answer by
Level 5

Hi @Parvathy_C ,

 

Please try below.

Map<String, Object> params = new HashMap<>();
params.put(resourceResolverFactory.SUBSERVICE, systemUserName);
ResourceResolver resourceResolver = mock(ResourceResolver.class);
ResourceResolverFactory resourceResolverFactory = mock(ResourceResolverFactory.class);
when(resourceResolverFactory.getServiceResourceResolver(params)).thenReturn(resourceResolver);

 

Thanks,

Sandeep.

View solution in original post

11 Replies

Avatar

Community Advisor

you can try something like this:

 

boolean isServiceUser =true;
User mockSystermUser = mock(User.class);
when(mockSystermUser.isSystemUser()).thenReturn(isServiceUser);
UserManager mockUserManager = mock(UserManager.class);
when(mockUserManager.getAuthorizable("<<<<systemUserName>>>>")).thenReturn(mockSystermUser);

Map<String, Object> authentication = new HashMap<>();
authentication.put("<<<<system user.name>>>", systemUserName);
ResourceResolver resourceResolver = context.getService(ResourceResolverFactory.class).getResourceResolver(authentication);

doReturn(mockUserManager).when(<<update condition>>>).getUserManager(resourceResolver.adaptTo(Session.class));

Avatar

Level 3

Thanks Suresh for your reply. 

Actually UserManager is deprecated. And getAuthorizable method doesnt exist now.

Also what do you mean by update condition? when(<<update condition>>>)

 

Thanks,

Parvathy

Avatar

Community Advisor
I mean to say, what you are going to test in update condition (i.e when (your class object is true)

Avatar

Community Advisor
Can you give your source code of the servlet and test class here

Avatar

Correct answer by
Level 5

Hi @Parvathy_C ,

 

Please try below.

Map<String, Object> params = new HashMap<>();
params.put(resourceResolverFactory.SUBSERVICE, systemUserName);
ResourceResolver resourceResolver = mock(ResourceResolver.class);
ResourceResolverFactory resourceResolverFactory = mock(ResourceResolverFactory.class);
when(resourceResolverFactory.getServiceResourceResolver(params)).thenReturn(resourceResolver);

 

Thanks,

Sandeep.

Avatar

Level 3

Hi Sandeep, Thanks for your reply. I tried like this, as you mentioned :

@Mock
private ResourceResolver resolver;
@Mock
private ResourceResolverFactory resolverFactory;

 

void test() throws RepositoryException, JSONException, ServletException, IOException{

Map<String, Object> params = new HashMap<>();
params.put(resolverFactory.SUBSERVICE, "testsystemuser");
when(resolverFactory.getServiceResourceResolver(params)).thenReturn(resolver);
mockContentOwnerServlet.doGet(ctx.request(), ctx.response());
assertEquals(ctx.response().getStatus(), SlingHttpServletResponse.SC_OK);

}

 

But I still get nullpointer exception in the class where the below line is mentioned:

ResourceResolver resourceResolver = resolverFactory.getServiceResourceResolver(AUTH_INFO);

 

Any way out ?

 

Thanks in advance!

Avatar

Level 5

I think you are getting null pointer for resourceResolverFactory.

 

Please add below line  mockContentOwnerServlet.setResourceResolverFactory(resourceResolverFactory); 

Avatar

Level 3
Yes, resourceResolverFactory is coming null mostly. But didn't quite get you. My servlet doesnt have setResourceResolverFactory method. How can i set resourceResolverFactory from testclass ?

Avatar

Level 5

Please add below lines your servlet. Then it will work.

 

private ResourceResolverFactory resourceResolverFactory;

@reference
public void setResourceResolverFactory(
ResourceResolverFactory resourceResolverFactory) {
this.resourceResolverFactory = resourceResolverFactory;
}

 

Thanks,

Sandeep.

Avatar

Level 3
Thanks, but then session comes null. Session session = CustomUtil.getResourceResolver(resourceResolverFactory).adaptTo(Session.class); Do I need to mock session also from test class?

Avatar

Level 3

As you mentioned, Added below line for session as well, worked.

when(resolver.adaptTo(Session.class)).thenReturn(mockSession);

Thanks Sandeep!