【问题标题】:Cannot resolve symbol InstantTaskExecutorRule无法解析符号 InstantTaskExecutorRule
【发布时间】:2018-06-11 11:02:56
【问题描述】:

我打开示例代码BasicRxJavaSample(来自这篇文章Room+RxJava) 最主要的是:

@Rule
public InstantTaskExecutorRule instantTaskExecutorRule = 
    new InstantTaskExecutorRule();

BasicRxJavaSample 一切正常。 但我不能在我的测试中应用它。 事情是这样的:

无法解析符号 InstantTaskExecutorRule

而且手动导入不起作用:

我的自动完成功能是这样的

但应该如此

我的应用 build.gradle (full gradle here):

// tests
testImplementation 'junit:junit:4.12'
androidTestCompile "com.android.support:support-annotations:$supportVersion"
testImplementation "android.arch.core:core-testing:$archVersion"
// Test helpers for Room
testImplementation "android.arch.persistence.room:testing:1.0.0"
// https://github.com/mockito/mockito
testImplementation 'org.mockito:mockito-core:2.13.0'
androidTestImplementation 'org.mockito:mockito-android:2.13.0'
// AndroidJUnitRunner and JUnit Rules
androidTestImplementation 'com.android.support.test:rules:1.0.1'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
// https://developer.android.com/topic/libraries/testing-support-library/packages.html
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
androidTestCompile 'com.android.support.test.espresso:espresso-idling-resource:3.0.1'

【问题讨论】:

    标签: android unit-testing junit4 rx-java2


    【解决方案1】:
    dependencies {
        testImplementation "androidx.arch.core:core-testing:2.1.0"
    }
    

    【讨论】:

      【解决方案2】:

      我知道已经晚了,但我想在这个已接受的答案中添加一件事。

      如果你想使用,

      @Rule
      public InstantTaskExecutorRule instantTaskExecutorRule = 
      new InstantTaskExecutorRule();
      

      在您的 JUnit 测试用例中,即在 test 文件夹 中,然后使用以下依赖项,即使用 testImplementation

      dependencies {
      testImplementation "android.arch.core:core-testing:1.0.0"
      }
      

      如果您想将InstantTaskExecutorRule 用于您的 UI 或集成测试用例(androidTest 文件夹),请使用 androidTestImplementation。那就是:

      androidTestImplementation "android.arch.core:core-testing:1.0.0"

      如果您想同时添加两者,请使用 androidTestImplementationtestImplementation,即:

      androidTestImplementation "android.arch.core:core-testing:1.0.0"

      testImplementation "android.arch.core:core-testing:1.0.0"

      对于 Android-X,请使用以下依赖项:

      androidTestImplementation 'androidx.arch.core:core-testing:2.0.0'

      testImplementation 'androidx.arch.core:core-testing:2.0.0'

      【讨论】:

      • 这应该是最好的答案
      • 感谢您指出@Kavita_p .... Android 大多是疯狂的,对开发人员不友好。如果系统需要在仪器测试和单元测试中具有依赖关系,则应定义 testImplementation 和 androidTestImplementation ...想知道 Google 什么时候会让所有这些对开发人员友好..!!!!我发现 IOS 开发对开发人员更加友好......这些想法真的很糟糕......
      • 但是如果你使用实现,这个库代码将出现在你的发布 APK 中。这不是必要的或正确的。如果你想同时使用,你应该同时使用 androidTestImplementation 和 testImplementation。
      • 嗨,@PauloPereira 感谢您的意见。是的,您在发布 APK 中包含这些库是正确的。我也会更新我的答案。
      【解决方案3】:

      将 testImplementation 替换为 androidTestImplementation。因此文件夹 androidTest 上的测试可以访问该库。

      dependencies {
          androidTestImplementation "androidx.arch.core:core-testing:2.1.0"
      }
      

      如果您不使用 androidx,请使用 android.arch.core:core-testing:1.1.1

      【讨论】:

      • 我已经进行了上述更改,但此解决方案不适用于我的情况。还有没有其他可能?
      • @Kavita_p 我将androidTestImplementation 更改为testImplementation,然后它解决并工作了
      • 如果在提供依赖项后不起作用。手动添加import androidx.arch.core.executor.testing.InstantTaskExecutorRule
      【解决方案4】:

      有时测试依赖问题可能是选择适当的构建变体的问题,具体取决于您的 Gradle 配置。在我的例子中,测试仅针对调试构建变体进行配置。

      【讨论】:

        【解决方案5】:

        对于androidX迁移,添加

        androidTestImplementation "androidx.arch.core:core-testing:2.0.0"
        

        【讨论】:

        【解决方案6】:

        我认为某些链接库中存在冲突。 我解决了这个问题,我使用了 blockingGet()blockingFirst()

        最后,我使用了 https://developer.android.com/training/testing/junit-runner.html#using-android-test-orchestrator

        androidTestUtil 'com.android.support.test:orchestrator:1.0.1'
        

        这就是你需要的!

        【讨论】:

          【解决方案7】:

          请将这两个依赖项放在你的 gradle 文件中,

          dependencies {
          
              // Test helpers for LiveData
              testImplementation "android.arch.core:core-testing:1.0.0"
          
              // Test helpers for Room
              testImplementation "android.arch.persistence.room:testing:1.0.0"
          }
          

          更多信息请通过此链接, Android Architecture components integration guide

          【讨论】:

            猜你喜欢
            • 2018-11-11
            • 1970-01-01
            • 1970-01-01
            • 2021-02-04
            • 2016-12-31
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多