我正在尝试使用 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
我只是通过将测试类放入
src \ test
文件夹而不是放入 src \ androidTest
文件夹 来解决问题
通常会有两种测试'androidTest'和'test'。
如果您添加像
androidTestImplementation 'org.robolectric:robolectric:4.3.1'
这样的依赖项,Robolectric 包将仅在“androidTest”类中导入。
如果您添加像
testImplementation 'org.robolectric:robolectric:4.3.1'
这样的依赖项,它们只能在“测试”类中导入。
我遵循了大部分建议,但没有任何运气。
然后我将点 (.) 更改为双冒号 (::)
是...
@RunWith(RobolectricTestRunner.class)
更改为...
@RunWith(RobolectricTestRunner::class)
RobolectricGradleTestRunner
已被弃用。只需使用@RunWith(RobolectricTestRunner.class)
查看官方文档。
我必须将版本从 4.13.2 移动(降级)到 3.3.2