Stephen Vinouze
Stephen Vinouze

Reputation: 2065

Robolectric with AndroidAnnotations make tests hang/not running

I am trying to configure a basic application with the Robolectric library. I am using the lattest version (3.0) as well as the lattest AndroidAnnotations version (3.3.2) and Gradle 1.4.0.

After struggling with few issues such as AndroidHttpClient exception, I managed to have it done with a very trivial test.

Now I wanted to further test with an Activity and I can't make the tests run. The tests hang somehow and never yield (yellow spin for each tests).

It seems to happen because of the @Before setup as it yields when commented. But why can't I setup my activity using this annotation? Especially while it yields when I use the annotated Activity (instead of the generated Activity_) which is not the proper way to test when using AndroidAnnotations.

Here is my truncated test class that still can't get executed...:

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = Build.VERSION_CODES.LOLLIPOP)
public class MapActivityTest {

  private MapActivity_ mMapActivity;

  @Before
  public void setup() {
    mMapActivity = Robolectric.buildActivity(MapActivity_.class).create().get();
  }

  @Test
  public void checkActivityNotNull() throws Exception {
    assertNotNull(mMapActivity);
  }

}

I am also using the Java 1.8 if that could be of any help.

Does anyone have any idea what could cause this issue?

Thanks already!

UPDATE:

Tried after downgrading to Java 7 and same result.

Upvotes: 1

Views: 1472

Answers (1)

nenick
nenick

Reputation: 7438

AndroidAnnotations is no issue for robolectric.

After some thread dumps I found the looping code. The class KBLocationProvider contains following snippet

boolean isPermissionGranted = ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED;
if (!isPermissionGranted) {          
    ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE_LOCATION);
}

And the checkSelfPermission(...) method never returns true. This method calls a checkPermission(..) methods on a context. A short search for checkPermission(..) showed a ShadowApplicationTest which shows the usage for checkPermission.

https://github.com/robolectric/robolectric/blob/867a805f36d7492ba80f150836bf88b97c3248e0/robolectric/src/test/java/org/robolectric/shadows/ShadowApplicationTest.java#L532

After adding following line your test is finishing.

@Before
public void setup() {
    Shadows.shadowOf(RuntimeEnvironment.application).grantPermissions(Manifest.permission.ACCESS_FINE_LOCATION);
    mMapActivity = Robolectric.buildActivity(MapActivity_.class).create().get();
}

Looks like you have an issue with an endless loop when the permission is denied.

Upvotes: 6

Related Questions