36

I have an application that reads SMSs. The app works fine when debugging but when testing it using android instrumented test it throws the following error

java.lang.SecurityException: Permission Denial: reading com.android.providers.telephony.SmsProvider

This is my test case

@RunWith(AndroidJUnit4.class)
public class SmsFetcherTest {

   @Test
   public void fetchTenSms() throws Exception {
      // Context of the app under test.
      Context appContext = InstrumentationRegistry.getContext();

      //   Fails anyway.
      //   assertTrue(ContextCompat.checkSelfPermission(appContext,
      //     "android.permission.READ_SMS") == PackageManager.PERMISSION_GRANTED);

      List<Sms> tenSms = new SmsFetcher(appContext)
              .limit(10)
              .get();

      assertEquals(10, tenSms.size());
   }
}

I'm new to instrumented tests. Is this is proper way to do this?

Or am I missing something?

rsanath
  • 1,154
  • 2
  • 15
  • 24

4 Answers4

43

Use GrantPermissionRule. Here's how:

Add the following dependency to app/build.gradle:

dependencies {
    ...
    androidTestImplementation 'androidx.test:rules:1.4.0'
}

Now add the following to your InstrumentedTest class:

import androidx.test.rule.GrantPermissionRule;

public class InstrumentedTest {
    @Rule
    public GrantPermissionRule mRuntimePermissionRule = GrantPermissionRule.grant(Manifest.permission.READ_SMS);
    ...
}
Daniel
  • 400
  • 1
  • 2
  • 12
donturner
  • 17,867
  • 8
  • 59
  • 81
  • 4
    [AndroidX](https://developer.android.com/reference/androidx/test/rule/GrantPermissionRule) link, dependency `androidTestImplementation 'androidx.test:rules:1.2.0'` – Stachu Jul 29 '20 at 10:00
  • 1
    For me seems like this rule doesn't matter, permissions validation is always returning true. – Akhha8 Sep 28 '21 at 23:03
10

You can grant the permission as follows:

@RunWith(AndroidJUnit4.class)
public class MyInstrumentationTest {
    @Rule
    public GrantPermissionRule permissionRule = GrantPermissionRule.grant(Manifest.permission.READ_SMS);
    ...

}
Sagar
  • 23,903
  • 4
  • 62
  • 62
1

If you use inheritance for instrumentation classes you should write @get:Rule in parent class.

For location:

import androidx.test.rule.GrantPermissionRule

@RunWith(AndroidJUnit4::class)
open class SomeTest {
    @get:Rule
    val permissionRule: GrantPermissionRule = GrantPermissionRule.grant(
        Manifest.permission.ACCESS_FINE_LOCATION,
        Manifest.permission.ACCESS_COARSE_LOCATION)

If you add ACCESS_BACKGROUND_LOCATION on device where Android < Q or permission that doesn't exist in AndroidManifest you will get an exception.

CoolMind
  • 26,736
  • 15
  • 188
  • 224
0

For reusability I create an interface:

interface WriteExternalPermission {

    @get:Rule val writeExternalPermission: GrantPermissionRule
        get() = GrantPermissionRule.grant(Manifest.permission.WRITE_EXTERNAL_STORAGE)

}

And you can just implement it inside your test class, where this permission is needed.

SerjantArbuz
  • 982
  • 1
  • 12
  • 16