MockitoJUnitRunner for junit in AemContext how to set user details | Community
Skip to main content
srinivas_chann1
Level 7
July 17, 2020
Solved

MockitoJUnitRunner for junit in AemContext how to set user details

  • July 17, 2020
  • 3 replies
  • 5383 views

Hi ,

 

I am using MockitoJUnitRunner for Junit .

 

I having the below code in java :-

User currentUser = request.getResourceResolver().adaptTo(User.class);

if(currentUser.isAdmin())
return;

Iterator<Group> currentUserGroups = currentUser.memberOf();

while (currentUserGroups.hasNext()) {
Group grp = (Group) currentUserGroups.next();
if(grp.getID().equals(GROUP)) {
-----
}
}

 

issue:-

 

So want to simulate the user and groups using Aemcontext and want to know how to set up user and group details for AEM context

 

I am using the below code 

@RunWith(MockitoJUnitRunner.class)
public class ComponentTestingTest {

@Rule
public final AemContext context = new AemContext(ResourceResolverType.JCR_MOCK);
}



Thanks

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Vijayalakshmi_S

Hi @srinivas_chann1,

I will try in my local and respond to this thread.

On a high level, there is no direct method under AemContext for UserManager/User related. We might have to mock and provide dummy implementation to the methods we call. 

 

3 replies

srinivas_chann1
Level 7
July 17, 2020

Sample junit code  will help

 

Thanks

Vijayalakshmi_S
Vijayalakshmi_SAccepted solution
Level 10
July 17, 2020

Hi @srinivas_chann1,

I will try in my local and respond to this thread.

On a high level, there is no direct method under AemContext for UserManager/User related. We might have to mock and provide dummy implementation to the methods we call. 

 

srinivas_chann1
Level 7
July 17, 2020
Hi , Any samples on dummy implementation will help. Thanks
ChitraMadan
Community Advisor
Community Advisor
July 17, 2020

Hi @srinivas_chann1 ,

 

May be try to do something like 

when(resourceResolver.getUserID()).thenReturn("anonymous"); 

or 

when(resourceResolver.getUserID()).thenReturn("admin");

or may be in your case, try, when(currentUser.isAdmin()).thenReturn("true");

 

Check out this Test Class where they have written tests based on anonymous or admin user

https://github.com/Adobe-Consulting-Services/acs-aem-commons/pull/1654/files#diff-82733893b715dd3d4c5e1cc60e9abe06