Testing Private methods with Power Mockito

87 Views Asked by At

I cant seem to successfully run the test case successfully.

i have and implementation class which calls a private method as follows:

@Service
public class PermissionServiceImpl implements PermissionService {

@Autowired
private PermissionRepository permissionRepository;

@Autowired
private UserRoleRepository roleRepository;

@Override
public Set<PermissionType> getPermissions(String accessToken, String clientId) {
    Map<String, List<String>> userRoles = getUserRoles(accessToken);
    List<UserRole> currRole = getCurrRole(userRoles, clientId);
    return getCurrentPermissions(currRole);
}

private Map<String, List<String>> getUserRoles(String accessToken) {
    Map<String, List<String>> roles = new HashMap<>();
    UserAccessTokenInfo userAccessTokenInfo = 
    SessionValidatorWithOKTA.getTokenMap().get(accessToken);
    if (userAccessTokenInfo != null) {
        roles = userAccessTokenInfo.getRoles();
    }
    return roles;
}

The above code is just a snippet. Havent given the full class due to confidentiality clause.

I have the following test case written:

@RunWith(PowerMockRunner.class)
@PrepareForTest(PermissionServiceImpl.class)
@ExtendWith(MockitoExtension.class)
public class PermissionServiceImplTest {

@InjectMocks
PermissionService permissionService = new PermissionServiceImpl();

@Mock
PermissionRepository permissionRepository;

@Mock
UserRoleRepository roleRepository;

@Test
public void testGetPermissions() {
    Set<PermissionType> permissionTypes = permissionService.getPermissions(ConstantsMock.ACCESS_TOKEN, ConstantsMock.CLIENT_ID);
    assertEquals(permissionTypes, new HashSet<>());
}

@Test
public void testGetPermissions_withRoles() throws Exception {
    Map<String, List<String>> roles = new HashMap();
    roles.put(ConstantsMock.CLIENT_ID, new ArrayList<>(Arrays.asList("admin")));
    PermissionServiceImpl mocked = PowerMockito.spy(new PermissionServiceImpl());
    PowerMockito.when(mocked, method(PermissionServiceImpl.class, "getUserRoles", String.class))
            .withArguments(anyString())
            .thenReturn(roles);

    mocked.getPermissions(ConstantsMock.ACCESS_TOKEN, ConstantsMock.CLIENT_ID)
 }
}

I am getting the following error:

Misplaced or misused argument matcher detected here:

 -> at 

  PermissionServiceImplTest.testGetPermissions_withRoles(PermissionServiceImplTest.java:51)

  You cannot use argument matchers outside of verification or stubbing.
  Examples of correct usage of argument matches:
  when(mock.get(anyInt())).thenReturn(null);
  doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject());
  verify(mock).someMethod(contains("foo"))

 This message may appear after an NullPointerException if the last matcher is returning an 
 object like any() but the stubbed method signature expect a primitive argument, in this case,
 use primitive alternatives.
 when(mock.get(any())); // bad use, will raise NPE
 when(mock.get(anyInt())); // correct usage use

Also, this error might show up because you use argument matchers with methods that cannot be 
mocked.
Following methods *cannot* be stubbed/verified: final/private/equals()/hashCode().
Mocking methods declared on non-public parent classes is not supported.

Can provide further details if needed.

0

There are 0 best solutions below