Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

MockitoJUnitRunner for junit in AemContext how to set user details

Avatar

Level 8

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

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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. 

 

View solution in original post

12 Replies

Avatar

Level 8

Sample junit code  will help

 

Thanks

Avatar

Correct answer by
Community Advisor

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. 

 

Avatar

Level 8
Hi , Any samples on dummy implementation will help. Thanks

Avatar

Community Advisor

Hi @srinivas_chann1,

Please find below sample Test case for the snippet posted in the query

Sample Model :

https://github.com/Viji1304/aemlearnings-samples/blob/master/learnings/core/src/main/java/learnings/...

Sample Test class :

https://github.com/Viji1304/aemlearnings-samples/blob/master/learnings/core/src/test/java/learnings/...

 

Flow goes like this:

  • UserManager, User and Group are mocked using MockitoExtension.class
  • Create adapterFactory for above 3 mocked APIs (aemContext.registerAdapter(adaptable, adapter, adapter object))
  • Dummy implementation using when and thenReturn methods.

Junit Version : 5

Maven Archetype : 22

AEM Version : 6.5.0

Note :

  • Couldn't do code coverage.
  • Along the similar lines of sample Test class file, request you to try for the actual code logic. If anything is not handled, update in this thread

Avatar

Level 8

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)

 

 

 

Avatar

Community Advisor

Hi @srinivas_chann1,

There are two ComponentContext one for cq:Component and other for OSGI.

  • com.day.cq.wcm.api.components.ComponentContext - (cq:Component)
  • org.osgi.service.component.ComponentContext - (OSGI component)

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.

Avatar

Level 8

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.

Avatar

Community Advisor

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());
	}

 

Avatar

Community Advisor

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