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
Solved! Go to Solution.
Views
Replies
Total Likes
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.
Sample junit code will help
Thanks
Views
Replies
Total Likes
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.
Views
Replies
Total Likes
Hi @srinivas_chann1,
Please find below sample Test case for the snippet posted in the query
Sample Model :
Sample Test class :
Flow goes like this:
Junit Version : 5
Maven Archetype : 22
AEM Version : 6.5.0
Note :
Views
Replies
Total Likes
Thanks for the input .I have still one pending issue will you be able to help on this
Java code:-
ComponentContext cc = WCMUtils.getComponentContext(slingHttpServletRequest);
cc.setDecorate(false);
Junit:-
I am trying
@Mock
private MockSlingHttpServletRequest request;
request = aemContext.request();
request.setAttribute("com.day.cq.wcm.componentcontext",aemContext componentContext());
Throws the below error
java.lang.ClassCastException: org.apache.sling.testing.mock.osgi.MockComponentContext cannot be cast to com.day.cq.wcm.api.components.ComponentContext
at com.day.cq.wcm.commons.WCMUtils.getComponentContext(WCMUtils.java:248)
Views
Replies
Total Likes
Hi @srinivas_chann1,
There are two ComponentContext one for cq:Component and other for OSGI.
Please share the functionality that you are achieving with cq:Component's ComponentContext. Based on that we will be able to write/assert tests.
Note : ComponentContext provided by AemContext is for OSGI and not for cq:Component.
Views
Replies
Total Likes
Hi,
Thanks for the response .
The use case is for a component dialog ,i would want to hide it based on groups .
This could be achieved by setting cq:noDecoration flag to true
This can be achieved by
ComponentContext cc = WCMUtils.getComponentContext(slingHttpServletRequest);
cc.setDecorate(false);
I see that the import statement is
com.day.cq.wcm.api.components.ComponentContext;
Thanks.
Views
Replies
Total Likes
Hi @srinivas_chann1,
Ok, getting.
Please try the below code and let know if it works. (Have pushed to GitHub too)
Spy of Mockito is used to call real implementation of a method. Hence we need not provide dummy implementation to the componentContext.
Please incorporate the test statements accordingly (per your code flow.)
@Spy
private ComponentContext spyCmpContext;
@Test
void testForComponentDecoration() {
MockSlingHttpServletRequest mockSlingRequest = aemContext.request();
mockSlingRequest.setAttribute(ComponentContext.CONTEXT_ATTR_NAME, spyCmpContext);
spyCmpContext.setDecorate(false);
assertFalse(spyCmpContext.hasDecoration());
}
Views
Replies
Total Likes
Views
Replies
Total Likes
Views
Replies
Total Likes
Hi @srinivas_chann1,
Sure, Have responded in the respective thread.
Views
Replies
Total Likes
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