MockitoJUnitRunner for junit in AemContext how to set user details

Avatar

Avatar
Validate 25
Level 4
srinivas_chann1
Level 4

Likes

21 likes

Total Posts

222 posts

Correct reply

6 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Ignite 5
Ignite 3
View profile

Avatar
Validate 25
Level 4
srinivas_chann1
Level 4

Likes

21 likes

Total Posts

222 posts

Correct reply

6 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Ignite 5
Ignite 3
View profile
srinivas_chann1
Level 4

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)

Avatar

Avatar
Boost 500
MVP
Vijayalakshmi_S
MVP

Likes

572 likes

Total Posts

718 posts

Correct reply

238 solutions
Top badges earned
Boost 500
Give Back 50
Give Back 5
Ignite 10
Ignite 5
View profile

Avatar
Boost 500
MVP
Vijayalakshmi_S
MVP

Likes

572 likes

Total Posts

718 posts

Correct reply

238 solutions
Top badges earned
Boost 500
Give Back 50
Give Back 5
Ignite 10
Ignite 5
View profile
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)

Avatar

Avatar
Boost 250
MVP
ChitraMadan
MVP

Likes

273 likes

Total Posts

165 posts

Correct reply

67 solutions
Top badges earned
Boost 250
Establish
Ignite 1
Give Back 5
Give Back 3
View profile

Avatar
Boost 250
MVP
ChitraMadan
MVP

Likes

273 likes

Total Posts

165 posts

Correct reply

67 solutions
Top badges earned
Boost 250
Establish
Ignite 1
Give Back 5
Give Back 3
View profile
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...