MockitoJUnitRunner for junit in AemContext how to set user details

srinivas_chann1

16-07-2020

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

 

Accepted Solutions (1)

Accepted Solutions (1)

Vijayalakshmi_S

MVP

17-07-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. 

 

Answers (1)

Answers (1)

ChitraMadan

MVP

17-07-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-82733893b715dd3d4c...