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);
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.
Solved! Go to Solution.
Views
Replies
Total Likes
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:
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.
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.
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:
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.
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.
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
Views
Likes
Replies