Junit for AccessControlUtil.getUserManager | Community
Skip to main content
Level 3
April 9, 2024
Solved

Junit for AccessControlUtil.getUserManager

  • April 9, 2024
  • 2 replies
  • 1071 views

 

Dear Team,

 

I have below method and want to write the JUNIT for below method.

 

************ JUNIT **************

 

@Mock UserManager userManager; Mockito.when(AccessControlUtil.getUserManager(session)).thenReturn(userManager);

 

 
************* JAVA ****************

 

private void createUserGroup(String groupName) throws RepositoryException { UserManager userManager = AccessControlUtil.getUserManager(session); Group testCommunityContentAuthorsBaseGroup = (Group) (userManager.getAuthorizable("test-community-authors")); if (userManager.getAuthorizable(groupName) == null) { Group group = userManager.createGroup(groupName); Value groupNameValue = session.getValueFactory().createValue(groupName, PropertyType.STRING); group.setProperty("./profile/givenName", groupNameValue); testCommunityContentAuthorsBaseGroup.addMember(userManager.getAuthorizable(groupName)); session.save(); logger.debug("Group successfully created: {}", group.getID()); } else { logger.debug("Group already exist: {} ", groupName); if (!testCommunityContentAuthorsBaseGroup.isMember(userManager.getAuthorizable(groupName))) { testCommunityContentAuthorsBaseGroup.addMember(userManager.getAuthorizable(groupName)); session.save(); logger.debug("Group successfully added to test-community-authors base group: {}", groupName); } } }

 

 

For above Java method when I am writing above JUNIT then I am getting below error.

 

 

Can anyone please let me know here , if I am missing anything.

 

Thanks in advance.

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 EstebanBustamante

Hi, 

The problem is that the way you are mocking the "AccessControlUtil" is incorrect. "Mockito.when" works for mocked types, but from what I can see in your code, the class is not being mocked. You have two options to fix this:

  1. Preferrable, you should not mock the "AccessControlUtil", but you should check what this method is doing and make sure your test works fine within this method. So, ideally, you should add the resources or necessary items for this method not to fail.

  2. You can mock the "AccessControlUtil" and then return a mock response. You could do something like this:

try (MockedStatic<AccessControlUtil> mocked = mockStatic(AccessControlUtil.class)) { mocked.when(() -> AccessControlUtil.getUserManager(any())).thenReturn(yourMockedObject); //Continue your test and your asserts below }

You can learn more here about mocking static methods: https://www.baeldung.com/mockito-mock-static-methods 

 

Hope this helps.

2 replies

EstebanBustamante
Community Advisor and Adobe Champion
EstebanBustamanteCommunity Advisor and Adobe ChampionAccepted solution
Community Advisor and Adobe Champion
April 9, 2024

Hi, 

The problem is that the way you are mocking the "AccessControlUtil" is incorrect. "Mockito.when" works for mocked types, but from what I can see in your code, the class is not being mocked. You have two options to fix this:

  1. Preferrable, you should not mock the "AccessControlUtil", but you should check what this method is doing and make sure your test works fine within this method. So, ideally, you should add the resources or necessary items for this method not to fail.

  2. You can mock the "AccessControlUtil" and then return a mock response. You could do something like this:

try (MockedStatic<AccessControlUtil> mocked = mockStatic(AccessControlUtil.class)) { mocked.when(() -> AccessControlUtil.getUserManager(any())).thenReturn(yourMockedObject); //Continue your test and your asserts below }

You can learn more here about mocking static methods: https://www.baeldung.com/mockito-mock-static-methods 

 

Hope this helps.

Esteban Bustamante
sravs
Community Advisor
Community Advisor
April 10, 2024

Hi @subnaik ,

 

Util class methods cannot be mocked as they are static. To mock these classes use MockStatic functionality. Please refer below to use mockstatic in different ways.

https://sourcedcode.com/blog/aem/junit/junit5/mocking-static-methods-in-junit5

https://mmarcosab.medium.com/mocking-static-methods-with-mockito-and-junit5-f108c841871e

https://www.baeldung.com/mockito-mock-static-methods