Utils { static boolean hasPermission(final Context context, String permission) { try { return PackageManager.PERMISSION_GRANTED == ContextCompat.checkSelfPermission(context, permission); } catch (Throwable t) { return false; } }    }
@Test public void testHasPermission() { ShadowApplication shadowApplication = Shadows.shadowOf(application); boolean actual = Utils.hasPermission(application, null); assertFalse("hasPermission must return false when permission is null", actual); shadowApplication.grantPermissions(Manifest.permission.ACCESS_FINE_LOCATION); boolean actualWhenPermissionGranted = Utils .hasPermission(application, Manifest.permission.ACCESS_FINE_LOCATION); assertTrue("hasPermission must return true when permission is granted", actualWhenPermissionGranted); shadowApplication.denyPermissions(Manifest.permission.ACCESS_FINE_LOCATION); boolean actualWhenPermissionDenied = Utils .hasPermission(application, Manifest.permission.ACCESS_FINE_LOCATION); assertFalse("hasPermission must return false when permission is denied", actualWhenPermissionDenied); }