Expand my Community achievements bar.

SOLVED

Junit for AccessControlUtils methods | Cannot read the array length because "<local4>" is null

Avatar

Level 4

Hi Everyone,

 

In my junit how can I pass this below line 

JackrabbitAccessControlList acl = AccessControlUtils.getAccessControlList(accessControlManager, "path");

 

at this line my junit is  getting an exception, so I want to ignore or pass this line. How can I control above line in my junit?

 

java.lang.NullPointerException: Cannot read the array length because "<local4>" is null
at org.apache.jackrabbit.commons.jackrabbit.authorization.AccessControlUtils.getAccessControlList(AccessControlUtils.java:138)
at

 

@Tanika02 @aanchal-sikka @ManviSharma @lukasz-m 

 

Thanks!!

 

 

1 Accepted Solution

Avatar

Correct answer by
Level 4

SOLUTION:

 

Hi @Tanika02  @salamswapnil @TarunKumar @kautuk_sahni ,@lukasz-m 

 

I am successfully able to pass the line JackrabbitAccessControlList acl = AccessControlUtils.getAccessControlList(accessControlManager, "path");

without getting an exception.

 

The code for passing above line without exception is :

 

@Mock
private AccessControlPolicyIterator accessControlPolicyIterator;

@Mock
private AccessControlManager accessControlManager;

@Mock
private AccessControlPolicy accessControlPolicy;

//To build policies
ImmutableList.Builder<AccessControlPolicy> policies = ImmutableList.builder();
//add mocked access Control Policy
policies.add(accessControlPolicy);
//build policy
List<AccessControlPolicy> l = policies.build();
//convert to array
AccessControlPolicy[] array= l.toArray(new AccessControlPolicy[0]);

//this is the behaviour for internal implementation of method getAccessControlList()
when(accessControlManager.getApplicablePolicies("path")).thenReturn(accessControlPolicyIterator);
when(accessControlPolicyIterator.hasNext()).thenReturn(true,false);
when(accessControlManager.getPolicies("path")).thenReturn(array);

 

 

Thanks for your time!!

View solution in original post

8 Replies

Avatar

Community Advisor

Hello @Uppari_Ramesh - 

 

To handle the exception and pass or ignore the mentioned line in your JUnit test, you can use a try-catch block. Here's an example of how you can control the line in your JUnit:

 

 

 

@Test
public void yourTestMethod() {
    try {
        JackrabbitAccessControlList acl = AccessControlUtils.getAccessControlList(accessControlManager, "path");
        // Rest of your test code that depends on 'acl'
    } catch (NullPointerException e) {
        // Handle the exception or ignore it
        // You can choose to leave this block empty if you want to ignore the exception
    }
}

 

 

 

By wrapping the problematic line inside the try block, any exceptions that occur during its execution will be caught by the catch block. In this case, a NullPointerException is caught. You can handle the exception within the catch block by providing custom logic or leave it empty to simply ignore the exception and continue with the rest of the test.

Avatar

Level 4

Hi @Tanika02 ,

 

Thanks for the reply! 

 

My requirement is different to solution that you have proposed. I want control above line junit file with when() and then().

 

Thanks!!

Avatar

Community Advisor

Hello @Uppari_Ramesh - 

 

import org.junit.Test;
import org.mockito.Mockito;

public class YourTestClass {

    @Test
    public void yourTestMethod() {
        // Create a mock of the AccessControlUtils class
        AccessControlUtils accessControlUtilsMock = Mockito.mock(AccessControlUtils.class);

        // Define the behavior of the mock
        Mockito.when(accessControlUtilsMock.getAccessControlList(Mockito.any(), Mockito.anyString()))
               .thenReturn(null);

        // Use the mock in your test
        // This will now return null instead of throwing a NullPointerException
        JackrabbitAccessControlList acl = accessControlUtilsMock.getAccessControlList(accessControlManager, "path");

        // Rest of your test code...
    }
}

Avatar

Level 4

Hi @Tanika02 ,

 

The method getAccessControlList(Session, String) is ambiguous for the type AccessControlUtils

 

This getAccessControlList() method will not accept argument matchers because this method is of type overloaded one i.e. there is one more method with same method.

Instead of any() we should give like any(AccessControlList.class) then we will not get ambiguity error.  

 

I have tried with argument matchers as well but it is still not taking mocked object but going inside implementation of that method @Tanika02 

 

The method getAccessControlList(any(), anyString()) is ambiguous for the type AccessControlUtils

 

Avatar

Level 6

@Uppari_Ramesh 

You can mock static methods with Mockito library. You need mockito-core and mockito-inline dependency for this.

@Mock
private AccessControlManager mockedAccessControlManager;

@Mock
private JackrabbitAccessControlList acl;

@Test
void testMethod() {
    try (MockedStatic<AccessControlUtils> util = Mockito.mockStatic(AccessControlUtils.class)) {
        util.when(() -> AccessControlUtils.getAccessControlList(mockedAccessControlManager, "path")).thenReturn(acl);
    }
}

 

Avatar

Level 4

Hi @salamswapnil , @Tanika02 

 

I have tried the solution that you have proposed but still the control is going inside the actual method getAccessControlList() and it is not taking the mocked object. Getting 

 

java.lang.NullPointerException: Cannot invoke "javax.jcr.security.AccessControlPolicyIterator.hasNext()" because "itr" is null
at org.apache.jackrabbit.commons.jackrabbit.authorization.AccessControlUtils.getAccessControlList(AccessControlUtils.java:129)

 

 

Note : I have added subsequent when and then conditions for conditions inside method getAccessControlList but getting 

 

java.lang.NullPointerException: Cannot read the array length because "<local4>" is null
at org.apache.jackrabbit.commons.jackrabbit.authorization.AccessControlUtils.getAccessControlList(AccessControlUtils.java:138)
at

Avatar

Community Advisor

Hi @Uppari_Ramesh ,

In path, can you trying passing the json resource that you might have loaded and also create specific nodes in the resource that you load. Lets see if that resolve your issue.

Avatar

Correct answer by
Level 4

SOLUTION:

 

Hi @Tanika02  @salamswapnil @TarunKumar @kautuk_sahni ,@lukasz-m 

 

I am successfully able to pass the line JackrabbitAccessControlList acl = AccessControlUtils.getAccessControlList(accessControlManager, "path");

without getting an exception.

 

The code for passing above line without exception is :

 

@Mock
private AccessControlPolicyIterator accessControlPolicyIterator;

@Mock
private AccessControlManager accessControlManager;

@Mock
private AccessControlPolicy accessControlPolicy;

//To build policies
ImmutableList.Builder<AccessControlPolicy> policies = ImmutableList.builder();
//add mocked access Control Policy
policies.add(accessControlPolicy);
//build policy
List<AccessControlPolicy> l = policies.build();
//convert to array
AccessControlPolicy[] array= l.toArray(new AccessControlPolicy[0]);

//this is the behaviour for internal implementation of method getAccessControlList()
when(accessControlManager.getApplicablePolicies("path")).thenReturn(accessControlPolicyIterator);
when(accessControlPolicyIterator.hasNext()).thenReturn(true,false);
when(accessControlManager.getPolicies("path")).thenReturn(array);

 

 

Thanks for your time!!