![](/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.