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
Solved! Go to Solution.
Views
Replies
Total Likes
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.
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));
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
Views
Replies
Total Likes
Views
Replies
Total Likes
Views
Replies
Total Likes
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!
Views
Replies
Total Likes
I think you are getting null pointer for resourceResolverFactory.
Please add below line mockContentOwnerServlet.setResourceResolverFactory(resourceResolverFactory);
Views
Replies
Total Likes
Views
Replies
Total Likes
Please add below lines your servlet. Then it will work.
private ResourceResolverFactory resourceResolverFactory;
@reference
public void setResourceResolverFactory(
ResourceResolverFactory resourceResolverFactory) {
this.resourceResolverFactory = resourceResolverFactory;
}
Thanks,
Sandeep.
Views
Replies
Total Likes
Views
Replies
Total Likes
As you mentioned, Added below line for session as well, worked.
when(resolver.adaptTo(Session.class)).thenReturn(mockSession);
Thanks Sandeep!
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies