Junit5 test case for accesscontrolManager functionality throws excpeption | Community
Skip to main content
March 25, 2021
Solved

Junit5 test case for accesscontrolManager functionality throws excpeption

  • March 25, 2021
  • 1 reply
  • 1547 views

This test case throws unsupported exception when using the JUNIT5 version as per below

I am instantiating the aem context at class level with below code

@ExtendWith({ AemContextExtension.class, MockitoExtension.class })

class TrashcanServletTest {

 

private final AemContext aemContext = new AemContext(ResourceResolverType.JCR_MOCK);

 

Testcase method is provided here :

 

void testDoGetSlingHttpServletRequestSlingHttpServletResponse() throws UnsupportedRepositoryOperationException, RepositoryException {

 

aemContext.create().page("/content/ey-unified-site/language-masters/en/blueprintpage2/page1", "/", "We Retail");

MockSlingHttpServletRequestmockSlingRequest = aemContext.request();

MockSlingHttpServletResponsemockSlingResponse = aemContext.response();

Resource payloadResource = mockSlingRequest.getResourceResolver().resolve("/content/ey-unified-site/language-masters/en/blueprintpage2/page1");

ResourceResolver rr = payloadResource.getResourceResolver();

    Session session = rr.adaptTo(Session.class);

    final AccessControlManager accessControlManager = session.getAccessControlManager();

    final Privilege moveToTrashCanPrivilege = accessControlManager.privilegeFromName(Privilege.JCR_REMOVE_NODE);

    if(accessControlManager.hasPrivileges(payloadResource.getPath(), new Privilege[]{moveToTrashCanPrivilege})) {

            //todo use full work

        }

 

This line-->     final AccessControlManager accessControlManager = session.getAccessControlManager(); throws java.lang.UnsupportedOperationException

 

Where as I found the test cases in below URL where JUNIT 4 class works

https://github.com/Adobe-Consulting-Services/acs-aem-commons/blob/master/bundle/src/test/java/com/adobe/acs/commons/wrap/jcr/BaseSessionIWrapTest.java

 Mocking the accessControlMager using when() method also not resolving the issue as per below code:

Mockito.lenient().when(session.getAccessControlManager()).thenReturn(accessControlManager);

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 Vijayalakshmi_S

Hi @subramanya_1310,

We need to

  • Mock the AccessControlManager and
  • In setUp()/@BeforeEach method, we need to write mock implementation - when(session.getAccessControlManager()).thenReturn(accessControlManager), as done in the code in Shared GITHUB path
    • Note : Junit5 equivalent for @Before is @BeforeEach
  • Use that mocked reference in the desired testMethod.

Per the code you have shared, problematic line is not using mocked AccessControlManager.

1 reply

Vijayalakshmi_S
Vijayalakshmi_SAccepted solution
March 25, 2021

Hi @subramanya_1310,

We need to

  • Mock the AccessControlManager and
  • In setUp()/@BeforeEach method, we need to write mock implementation - when(session.getAccessControlManager()).thenReturn(accessControlManager), as done in the code in Shared GITHUB path
    • Note : Junit5 equivalent for @Before is @BeforeEach
  • Use that mocked reference in the desired testMethod.

Per the code you have shared, problematic line is not using mocked AccessControlManager.

Vijayalakshmi_S
March 25, 2021

@subramanya_1310,

No worries. Can you share your complete test class if possible. 

In parallel, will try to reproduce in my local. 

 

@kautuk_sahni,

Could you please mark this thread as unresolved.