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

Avatar

Avatar

Parvathy_C

Avatar

Parvathy_C

Parvathy_C

10-11-2020

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

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar

Sandeep6

Avatar

Sandeep6

Sandeep6

10-11-2020

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.

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!

I think you are getting null pointer for resourceResolverFactory.

 

Please add below line  mockContentOwnerServlet.setResourceResolverFactory(resourceResolverFactory); 

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 ?

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

 

private ResourceResolverFactory resourceResolverFactory;

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

 

Thanks,

Sandeep.

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

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

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

Thanks Sandeep!

Answers (1)

Answers (1)

Avatar

Avatar

sureshd71454102

Avatar

sureshd71454102

sureshd71454102

10-11-2020

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));