簡體   English   中英

使用Robolectric運行JUnit測試時出現Gradle和Android Studio問題

[英]Gradle and Android Studio issue when running JUnit tests using Robolectric

該應用程序的build.gradle為:

    apply plugin: 'com.android.application'

    android {
        compileSdkVersion 22
        buildToolsVersion "22.0.0"

    defaultConfig {
        applicationId "myapp.robolectricexample2"
        minSdkVersion 14
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

    dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:22.0.0'
    compile 'org.robolectric:robolectric:3.0-rc2'
    compile 'junit:junit:4.12'
}

因此,出現以下錯誤:

錯誤:等級:任務':app:dexDebug'的執行失敗。 > com.android.ide.common.process.ProcessException:org.gradle.process.internal.ExecException:處理'命令'C:\\ Program Files \\ Java \\ jdk1.8.0_31 \\ bin \\ java.exe' -零退出值2

試試這個build.gradle

android {
    apply plugin: 'com.android.application'

    android {
        compileSdkVersion 22
        buildToolsVersion "22.0.1" // <-- was 22.0.0

    defaultConfig {
        applicationId "myapp.robolectricexample2"
        minSdkVersion 14
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile 'com.android.support:appcompat-v7:22.1.1' // <-- was 22.0.0
    testCompile 'junit:junit:4.12'
    testCompile 'org.robolectric:robolectric:3.0-rc2'
}

現在,您可以運行gradlew testDebug

將您的測試放在src/test

我也有這個問題。 就我而言,我發現原因是我將測試文件放在錯誤的目錄中。

我以前的項目結構是:

app
|-src
|  |- main
|  |- androidTest <- I put here.
|  |- test <- The test files should be put here.
|-build 

我將測試文件移至android默認單元測試目錄src / test / java,然后問題解決了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM