Expand my Community achievements bar.

SOLVED

Junit for AccessControlUtil.getUserManager

Avatar

Level 4

 

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.

 

subnaik_0-1712653188451.png

 

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

 

Thanks in advance.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

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