Granting Permissions
JUnit5 apparently does not support
@Rule
: http://www.douevencode.com/articles/2018-06/grant-permission-no-rule/ “JUnit5, which is ultimately the future discourages to use@Rules
"With the above said, we can hack in a workaround to be able to grant permissions:
import androidx.test.runner.permission.PermissionRequester; public class SomeClass { // Hacky workaround for JUnit5 not supporting @Rule private void GrantPermissions(String ... permissions) { PermissionRequester pr = new PermissionRequester(); pr.addPermissions(permissions); pr.requestPermissions(); } }
Usage:
public class SomeClass { //... @Test public void SomeExampleTest() { GrantPermissions(android.Manifest.permission.BLUETOOTH_SCAN); VictorBleDeviceManager dm = new VictorBleDeviceManager(ApplicationProvider.getApplicationContext()); List<VictorDeviceInfo> scannedDevices = dm.ScanForDevices().get(); assertTrue(scannedDevices.isEmpty()); } }