我正在尝试使用 InstrumentationTestCase 测试我的类(class)。我一直在检查示例,但我想我遗漏了一些东西,因为“测试 Artifact ”下拉列表被禁用并且无法更改为 JUNit。
我在gradle中声明了库
// Test Compile
testCompile 'junit:junit:4.12'
testCompile 'org.robolectric:robolectric:2.4'
testCompile 'org.mockito:mockito-core:1.9.5'
testCompile 'org.reflections:reflections:0.9.9'
声明的应用程序ID,
defaultConfig {
testApplicationId "myproject.android.test"
}
并获取最新的gradle版本
dependencies {
classpath 'com.android.tools.build:gradle:+'
}
还为我的测试包创建了构建配置。但是当我运行测试时它给了我
Exception in thread "main" java.lang.NoClassDefFoundError: junit/textui/ResultPrinter
可能是因为测试 Artifact 是“Android Instrumentation Tests”,构建配置是 JUNit 配置。测试 Artifact 下拉列表已禁用,因此我无法将其更改为 JUNit。有什么我想念的吗?
编辑: 我的包树是这样的,
myproject
- app
- main
- java
-myproject
-android
-mypackage
-myclass.java
-test
-java
-myproject
-android
-mypackage
-test
-myclasstest.java
并且在 Android Studio 首选项中,在 Gradle->Experimental 下,选中“启用单元测试支持”。
最佳答案
您需要执行几个步骤才能在 Android Studio 中激活 JVM 单元测试。按照以下步骤操作:https://sites.google.com/a/android.com/tools/tech-docs/unit-testing-support
关于Android Studio gradle实验测试(JUNit),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29389175/