muetzenflo muetzenflo - 3 years ago 110
Android Question

Robolectric + Mockito + Marshmallow + Permissions

I am trying to write a unit test for a method that depends on the result of

ActivityCompat.checkSelfPermission(..)
. To do so, I need to do two things:


  1. call
    Robolectric.shadowOf(activity.getApplication).grantPermission(...)


    which depends on an Activity built by
    Robolectric.buildActivity(PictureActivity.class).get()

  2. call
    Mockito.verify(activity).someMethod(wasCalled)


    which depends on an Activity built by

    Mockito.mock(MyActivity.class)



So when I create my Activity with Robolectric, I cannot use the Mockito.verify.
And when I create my Activity with Mockito, I cannot use the Robolectric.grantPermission.

How can I cover this? I'm new with both frameworks, so maybe I am missing something simple.

Answer Source

Haven't checked it out in detail, but since today it should be possible to make use of GrantPermissionRule by using the new Android Testing Support Library

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download