简体   繁体   English

具有AndroidAnnotations的Robolectric使测试挂起/未运行

[英]Robolectric with AndroidAnnotations make tests hang/not running

I am trying to configure a basic application with the Robolectric library. 我正在尝试使用Robolectric库配置基本应用程序。 I am using the lattest version (3.0) as well as the lattest AndroidAnnotations version (3.3.2) and Gradle 1.4.0. 我正在使用最新版本(3.0)以及最新版本AndroidAnnotations版本(3.3.2)和Gradle 1.4.0。

After struggling with few issues such as AndroidHttpClient exception, I managed to have it done with a very trivial test. 在解决了诸如AndroidHttpClient异常之类的少数问题之后,我设法通过一个非常简单的测试来完成它。

Now I wanted to further test with an Activity and I can't make the tests run. 现在,我想对一个Activity进行进一步测试,但我无法运行测试。 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. 这似乎是由于@Before设置引起的,因为它在注释时产生。 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. 特别是当我使用带注释的Activity(而不是生成的Activity_)时,它会产生收益,这不是使用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. 我也正在使用Java 1.8,如果有帮助的话。

Does anyone have any idea what could cause this issue? 有谁知道什么可能导致此问题?

Thanks already! 已经谢谢你了!

UPDATE: 更新:

Tried after downgrading to Java 7 and same result. 降级到Java 7后尝试,结果相同。

AndroidAnnotations is no issue for robolectric. AndroidAnnotations对于robolectric来说不是问题。

After some thread dumps I found the looping code. 经过一些线程转储后,我发现了循环代码。 The class KBLocationProvider contains following snippet KBLocationProvider类包含以下代码段

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. 并且checkSelfPermission(...)方法从不返回true。 This method calls a checkPermission(..) methods on a context. 此方法在上下文上调用checkPermission(..)方法。 A short search for checkPermission(..) showed a ShadowApplicationTest which shows the usage for checkPermission. 简短搜索 checkPermission(..)显示了ShadowApplicationTest,其中显示了checkPermission的用法。

https://github.com/robolectric/robolectric/blob/867a805f36d7492ba80f150836bf88b97c3248e0/robolectric/src/test/java/org/robolectric/shadows/ShadowApplicationTest.java#L532 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. 权限被拒绝时,您似乎遇到了无限循环的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM