【问题标题】:"RobolectricTestRunner.class" not recognized in Android StudioAndroid Studio 无法识别“RobolectricTestRunner.class”
【发布时间】:2017-11-14 11:24:25
【问题描述】:

我正在尝试使用 Robolectric 运行单元测试,但 Android Studio 无法识别以下类别:

@RunWith(RobolectricTestRunner.class)

详情:

classpath 'com.android.tools.build:gradle:3.0.0'

我正在导入依赖项:

testImplementation "org.robolectric:robolectric:3.5.1"

还有:

android {
    testOptions {
        unitTests {
            includeAndroidResources = true
        }
    }
}

它根本没有出现导入类的选项。 我应该添加更多我忘记的依赖项吗?

错误:

 error: package org.robolectric does not exist
 error: package org.robolectric does not exist
 error: package org.robolectric.shadows does not exist
 error: cannot find symbol class RobolectricTestRunner
 error: cannot find symbol variable Robolectric
 error: cannot find symbol variable ShadowApplication

【问题讨论】:

  • 您可以尝试使用 testImplementation 而不是 testCompile。现在使用 gradle 版本 4 已弃用
  • @Fakher 错误与 androidTestImplementation 相同,但无法通过 testImplementation 识别。我使用: androidTestImplementation 'com.android.support.test:runner:1.0.1' 并且它在同一个项目上正常工作,只是不工作的机器人。
  • Robolectric 始终是 test 而不是 androidTest 依赖项

标签: android unit-testing android-gradle-plugin robolectric robolectric-gradle-plugin


【解决方案1】:

我只是通过将测试类放在src \ test 文件夹中而不是将其放在src \ androidTest 文件夹中解决了这个问题

【讨论】:

  • 老兄......你救命
  • @JacktheRipper LOL,很高兴能帮上忙!!
  • 非常好。非常感谢。但是为什么会这样呢?有没有办法将其配置为接受 src\androidTest?
  • 很好!!有帮助!!
  • 我花了 1 个小时才解决了这个问题,直到我找到了你的答案,谢谢。
【解决方案2】:

RobolectricGradleTestRunner 自 Robolectric 版本 3 起已弃用。只需使用@RunWith(RobolectricTestRunner.class) 看看官方documentation

【讨论】:

  • 我遵循了官方文档,但错误仍在继续......还有其他建议吗?多一些依赖,我不知道……
【解决方案3】:

通常会有2种测试'androidTest'和'test'。

如果添加androidTestImplementation 'org.robolectric:robolectric:4.3.1' 之类的依赖项,Robolectric 包将仅在“androidTest”类中​​导入。

如果添加 testImplementation 'org.robolectric:robolectric:4.3.1' 之类的依赖项,则只能在“测试”类中导入。

【讨论】:

  • 我刚刚添加了 androidTestImplementation 和 testImplementation
【解决方案4】:

我没有任何运气地遵循了大多数建议。
然后我将点 (.) 更改为双冒号 (::)

曾经...

@RunWith(RobolectricTestRunner.class)

改为...

@RunWith(RobolectricTestRunner::class)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-27
    • 2013-05-10
    • 2021-05-06
    • 2019-01-28
    • 2021-03-27
    相关资源
    最近更新 更多