![](/img/trans.png)
[英]Android Studio + Robolectric + Gradle Class Not Found Exception
[英]Android project with Robolectric and Gradle (Android studio)
我正在尝试使用Robolectric在一个项目构建中使用gradle在新的ide for android:Android studio中,但我面临一个奇怪的问题,我已经正确导入了所有库并在“src”中创建了“test”文件夹“,事实是每当我运行测试时,ide会继续说”Class not found:“com.example.myandroidproject.test”我做错了什么?我需要在gradle.build中更改一些内容吗?这是我的目录结构体:
@Aldo Borrero,最后似乎有人找到了使用Robolectric和Gradle在“Android Studio”下测试android项目的方法。 请看看这个答案Robolectric with Gradle
更新:来自广场的人们发布了一个插件,让Robolectric与Gradle和Android Studio一起开箱即用,这个功能将在v2中与Robolectric集成,同时你可以在这里获取插件: Gradle Android测试插件
我尝试了不同的appraoaches结合android studio&robolectric和espresso。 我结束了这个示例项目设置https://github.com/nenick/android-gradle-template
这里有一些不同方法的解释:
robolectric维护者支持一个示例https://github.com/robolectric/deckard-gradle 。 这是基于插件https://github.com/robolectric/gradle-android-test-plugin 。 但这有一个缺点,在https://github.com/robolectric/gradle-android-test-plugin/issues/17上报告了依赖性污染,导致esspresso测试编译时间和执行时间变慢。
buildscript {
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.10.+'
classpath 'org.robolectric.gradle:gradle-android-test-plugin:0.10.+'
}
}
apply plugin: 'android'
apply plugin: 'android-test'
android {
defaultConfig {
testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
}
}
androidTest {
include '**/*Test.class'
exclude '**/espresso/**/*.class'
}
dependencies {
androidTestCompile('junit:junit:4.11')
androidTestCompile('org.robolectric:robolectric:2.3-SNAPSHOT')
androidTestCompile 'com.jakewharton.espresso:espresso:1.1-r2'
}
一个例子由https://github.com/stephanenicolas/Quality-Tools-for-Android显示,但它已经过时并且也有一些缺点。 它将重新编译并使android studio表现得很奇怪。 它将应用程序模块源标记为espresso测试模块的(根源)。 这有效但不直观。
dependencies {
androidTestCompile 'com.jakewharton.espresso:espresso:1.1-r2'
}
android {
sourceSets {
main {
manifest.srcFile '../AndroidSample/AndroidManifest.xml'
java.srcDirs += ['../AndroidSample/src/main/java']
resources.srcDirs = ['../AndroidSample/res']
res.srcDirs = ['../AndroidSample/res']
}
}
defaultConfig {
testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
}
}
存在一个插件https://github.com/novoda/gradle-android-test-plugin ,它使我们能够将robolectric测试放入sperate包中。 这个项目设置对我很有用:
- MyProject
|- app (with espresso tests)
|- - build.gradle (app)
|- robolectric (unit tests)
|- - build.gradle (robo)
dependencies {
androidTestCompile 'com.jakewharton.espresso:espresso:1.1-r2'
}
android {
defaultConfig {
testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
}
}
buildscript {
repositories {
mavenCentral()
maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
}
dependencies {
classpath 'com.android.tools.build:gradle:0.9.+'
classpath "com.novoda:gradle-android-test-plugin:0.9.8-SNAPSHOT"
}
}
android {
projectUnderTest ':AndroidSample'
}
apply plugin: 'java'
apply plugin: 'android-test'
dependencies {
testCompile 'junit:junit:4.11'
testCompile 'org.mockito:mockito-core:1.9.5'
testCompile 'com.squareup:fest-android:1.0.+')
testCompile ('org.robolectric:robolectric:2.3-SNAPSHOT')
}
尝试设置此项目设置时会有一些陷阱,所以只需从一个工作示例开始: https : //github.com/nenick/android-gradle-template
这不太可能开箱即用,因为src / test没有自动使用。 您需要自动创建一个测试任务,编译此源集,设置正确的依赖项并运行它。
我们打算在将来支持这一点,但现在您需要手动执行此操作。
我测试了这里提供的所有解决方案,它们都缺少某些东西(不支持gradle / gradle插件版本,不支持库项目,不与Android studio集成等)。 未来可能不是这样,但今天却是如此。
我发现的最好方法是自己配置单元测试。 您需要在build.gradle文件中添加几行配置。 有关以下文章的解释: http : //tryge.com/2013/02/28/android-gradle-build/ 。 由于我不是作者,我认为我不能直接复制这里的内容。
除了那篇文章之外,如果你想配置Android Studio将单元测试文件夹看作源文件夹(自动完成和东西),你可以应用下面的小脏攻,让IDE认为单元测试位于instrumentationTest中夹。 当然,它会弄乱您的真实仪器测试,所以只有当您没有任何这些测试时它才有效。
的build.gradle
// the unit test source set as described in the article
sourceSets {
unitTest {
java.srcDir file('src/test/java')
resources.srcDir file('src/test/resources')
}
}
android {
// tell Android studio that the instrumentTest source set is located in the unit test source set
sourceSets {
instrumentTest.setRoot('src/test')
}
}
dependencies {
// your unit test dependencies as described in the article
unitTestCompile files("$project.buildDir/classes/release")
unitTestCompile 'junit:junit:4.11'
unitTestCompile 'com.google.android:android:4.1.1.4'
unitTestCompile 'org.robolectric:robolectric:2.1.1'
// duplicate these dependencies in the instrumentTestCompile scope
// in order to have the integration in Android Studio (autocompletion and stuff)
instrumentTestCompile 'junit:junit:4.11'
instrumentTestCompile 'org.robolectric:robolectric:2.1.1'
}
// the rest of the config as described in the article
测试Android Studio 0.2.6和android gradle插件0.5。
Gradle Android Unit Testing Plugin对我来说是最好的选择。
由Jake Wharton开发,我想它将成为下一个标准(可能直到谷歌发布Android Studio中对Robolectric的开箱即用支持)。
您可以通过添加build.gradle
文件来导入库:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.X.+'
classpath 'com.squareup.gradle:gradle-android-test-plugin:0.9.+'
}
}
...
apply plugin: 'android-test'
...
dependencies {
testCompile 'junit:junit:4.10'
testCompile 'org.robolectric:robolectric:2.1.+'
testCompile 'com.squareup:fest-android:1.0.+'
}
更新:自gradle插件版本0.8以来,此库已被弃用
我已经测试了很多场景(例如http://tryge.com/2013/02/28/android-gradle-build/和http://www.peterfriese.de/android-testing-with-robolectric/ )但只有Robolectric团队提供的解决方案才能为我工作。 该设置在一个Android项目中使用了instrumented和robolectric测试,并将gradle作为构建系统。
请参阅http://robolectric.org/getting_started/以及https://github.com/robolectric/deckard-gradle上的来源
buildscript {
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.9.2'
classpath 'org.robolectric.gradle:gradle-android-test-plugin:0.9.4'
}
}
allprojects {
repositories {
mavenCentral()
}
}
apply plugin: 'android'
apply plugin: 'android-test'
android {
packagingOptions {
exclude 'LICENSE.txt'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE'
}
compileSdkVersion 19
buildToolsVersion "19.0.3"
defaultConfig {
minSdkVersion 18
targetSdkVersion 18
versionCode 2
versionName "1.0.0-SNAPSHOT"
testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
}
buildTypes {
release {
runProguard false
}
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
res.srcDirs = ['res']
}
androidTest {
setRoot('src/test')
}
}
}
androidTest {
include '**/*Test.class'
exclude '**/espresso/**/*.class'
}
dependencies {
repositories {
mavenCentral()
maven {
url 'https://oss.sonatype.org/content/repositories/snapshots/'
}
}
// Espresso
androidTestCompile files('lib/espresso-1.1.jar', 'lib/testrunner-1.1.jar', 'lib/testrunner-runtime-1.1.jar')
androidTestCompile 'com.google.guava:guava:14.0.1',
'com.squareup.dagger:dagger:1.1.0',
'org.hamcrest:hamcrest-integration:1.1',
'org.hamcrest:hamcrest-core:1.1',
'org.hamcrest:hamcrest-library:1.1'
androidTestCompile('junit:junit:4.11') {
exclude module: 'hamcrest-core'
}
androidTestCompile('org.robolectric:robolectric:2.3-SNAPSHOT') {
exclude module: 'classworlds'
exclude module: 'maven-artifact'
exclude module: 'maven-artifact-manager'
exclude module: 'maven-error-diagnostics'
exclude module: 'maven-model'
exclude module: 'maven-plugin-registry'
exclude module: 'maven-profile'
exclude module: 'maven-project'
exclude module: 'maven-settings'
exclude module: 'nekohtml'
exclude module: 'plexus-container-default'
exclude module: 'plexus-interpolation'
exclude module: 'plexus-utils'
exclude module: 'wagon-file'
exclude module: 'wagon-http-lightweight'
exclude module: 'wagon-http-shared'
exclude module: 'wagon-provider-api'
}
androidTestCompile 'com.squareup:fest-android:1.0.+'
}
apply plugin: 'idea'
idea {
module {
testOutputDir = file('build/test-classes/debug')
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.