我正在 Android Studio 中进行一些单元测试(未检测)。我的所有测试依赖项都已在“build.gradle”中声明,如下所示:
testImplementation 'junit:junit:4.12'
testImplementation "org.mockito:mockito-core:$mockitoVersion"
testImplementation "org.powermock:powermock-module-junit4:$powerMockVersion"
testImplementation "org.powermock:powermock-api-mockito2:$powerMockVersion"
testImplementation 'org.json:json:20140107'
testImplementation 'org.robolectric:robolectric:3.4.2'
Run Code Online (Sandbox Code Playgroud)
但是,当我加载测试文件时,所有依赖项都被标记为“无法解析符号 XXX”。如果我将其从 更改testImplementation为androidTestImplementation,则所有依赖项都会被正确检测到。然而,这些不是仪器测试,而是独立的单元测试,所以当我用它执行测试时,androidTestImplementation它会返回一个error: package org.junit does not exist,阻止我运行单元测试。到目前为止我已经检查过的事情(来自许多 stackoverflow 答案):
test文件夹中,而不是androidTest.idea并.gradle文件夹另外,需要注意(或者可能不是)该项目是一个库而不是一个应用程序,这一点很重要。
作为一种解决方法,我已经声明了依赖项,这样implementation我就可以继续在本地运行测试,但这当然很麻烦而且很危险,因为我不想最终错误地运送带有测试依赖项的 AAR。