### Question:
PermissionCheckService { public Object hasPermission(ProceedingJoinPoint proceedingJoinPoint) { long startTime = System.currentTimeMillis(); MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature(); LOGGER.debug("Permission check started at {} (method: {})", startTime, methodSignature.getMethod().getDeclaringClass().getSimpleName() + "#" + methodSignature.getMethod().getName()); Optional<Object> initiatorUserCrn = reflectionUtil.getParameter(proceedingJoinPoint, methodSignature, InitiatorUserCrn.class); if (InternalCrnBuilder.isInternalCrn(ThreadBasedUserCrnProvider.getUserCrn()) && initiatorUserCrn.isPresent() && initiatorUserCrn.get() instanceof String && Crn.isCrn((String) initiatorUserCrn.get())) { String newUserCrn = (String) initiatorUserCrn.get(); internalUserModifier.persistModifiedInternalUser(crnUserDetailsService.loadUserByUsername(newUserCrn)); return ThreadBasedUserCrnProvider.doAs(newUserCrn, () -> commonPermissionCheckingUtils.proceed(proceedingJoinPoint, methodSignature, startTime)); } if (commonPermissionCheckingUtils.isAuthorizationDisabled(proceedingJoinPoint) || InternalCrnBuilder.isInternalCrn(ThreadBasedUserCrnProvider.getUserCrn())) { return commonPermissionCheckingUtils.proceed(proceedingJoinPoint, methodSignature, startTime); } checkPrerequisitesOnClass(proceedingJoinPoint, methodSignature); return checkPermission(proceedingJoinPoint, methodSignature, startTime); }  @PostConstruct void populatePermissionCheckMap(); Object hasPermission(ProceedingJoinPoint proceedingJoinPoint);  }### Answer:
@Test public void testHasPermissionIfAuthorizationDisabled() throws NoSuchMethodException { when(methodSignature.getMethod()).thenReturn(ExampleClass.class.getMethod("disabledAuthorization")); when(commonPermissionCheckingUtils.proceed(any(), any(), anyLong())).thenReturn(null); ThreadBasedUserCrnProvider.doAs(USER_CRN, () -> underTest.hasPermission(proceedingJoinPoint)); verify(permissionChecker, times(0)).checkPermissions(any(), anyString(), any(), any(), anyLong()); }
@Test public void testHasPermissionIfThereAreTooManyAnnotationOnMethod() throws NoSuchMethodException { when(methodSignature.getMethod()).thenReturn(ExampleClass.class.getMethod("tooManyAnnotation")); ThreadBasedUserCrnProvider.doAs(USER_CRN, () -> underTest.hasPermission(proceedingJoinPoint)); verify(permissionChecker, times(0)).checkPermissions(any(), anyString(), any(), any(), anyLong()); verify(commonPermissionCheckingUtils).proceed(any(), any(), anyLong()); }
@Test public void testHasPermission() throws NoSuchMethodException { when(methodSignature.getMethod()).thenReturn(ExampleClass.class.getMethod("correctMethod")); when(permissionChecker.supportedAnnotation()).thenReturn(CheckPermissionByAccount.class); doNothing().when(permissionChecker).checkPermissions(any(), any(), any(), any(), anyLong()); when(commonPermissionCheckingUtils.proceed(any(), any(), anyLong())).thenReturn(null); underTest.populatePermissionCheckMap(); ThreadBasedUserCrnProvider.doAs(USER_CRN, () -> underTest.hasPermission(proceedingJoinPoint)); verify(permissionChecker).checkPermissions(any(), any(), any(), any(), anyLong()); verify(commonPermissionCheckingUtils).proceed(any(), any(), anyLong()); }