2

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.

muetzenflo
  • 5,653
  • 4
  • 41
  • 82
  • I would wrap logic in some permission checker and mock it in activity tests. However, the testing of this permission checker will bring the same problem. I will check if Robolectric supports anything with permissions otherwise I think you need to write own shadow – Eugen Martynov Feb 15 '16 at 16:04
  • Why are you calling activity.getApplication() when you could be using Robolectric.application instead? – robinj Feb 18 '16 at 12:21
  • 1
    @robinj because then I would grant permissions to the wrong application. see http://stackoverflow.com/questions/35031301/android-robolectric-unit-test-for-marshmallow-permissionhelper – muetzenflo Feb 18 '16 at 14:43
  • Try this it may be work http://stackoverflow.com/a/41221852/5488468 – Bipin Bharti Jan 03 '17 at 10:48

2 Answers2

1

Read my answer on https://stackoverflow.com/a/37704872/1345391 in which I explain a way to workaround the checkSelfPermission problem with robolectric

Community
  • 1
  • 1
JavierSP1209
  • 899
  • 8
  • 17
1

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

muetzenflo
  • 5,653
  • 4
  • 41
  • 82