新 gradle 和 android sdk 版本的 Robolectric 问题

Roboelectric issues with new gradle and android sdk versions

直到最近,我都可以将 roboelectric 与 gradle 一起使用。我一直收到错误 Error:(6, 17) error: package org.junit does not exist。我不太确定并且对此进行了相当多的研究。

下面是我的项目build.gradle:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        mavenCentral()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.2.3'
    }
}

allprojects {
    repositories {
        mavenCentral()
        jcenter()
    }
}

下面是我的应用程序 build.gradle:

repositories {
    mavenCentral()
    jcenter()
}

apply plugin: 'com.android.application'

android {
    ...

    sourceSets {
        androidTest.setRoot('src/test')
    }
}


dependencies {
    ...

    // Testing
    compile project(':core')
    testCompile 'org.robolectric:robolectric:2.4'
    testCompile 'junit:junit:4.+'
    testCompile 'org.easytesting:fest:1.0.16'
    testCompile 'com.squareup:fest-android:1.0.8'
}

我的核心项目build.gradle:

apply plugin: 'java'

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    testCompile 'org.robolectric:robolectric:2.4'
}

我已阅读以下内容,但对我没有任何帮助:

https://www.bignerdranch.com/blog/all-in-together-android-studio-gradle-and-robolectric/ - 要使用的 android 工作室插件在较新的 android 工作室版本上崩溃。

https://www.bignerdranch.com/blog/triumph-android-studio-1-2-sneaks-in-full-testing-support/ - 这根本不能解决问题。它找不到 org.junit.

https://discuss.gradle.org/t/why-cant-my-simplest-one-line-build-gradle-compile-tests-with-junit-jar/1868

任何人都可以为此指出正确的方向吗?为什么无法从 build.gradle 中检测到 org.junit

您必须将构建变体测试工件设置为 Unit Tests

我不再需要 "core" 项目,所以我删除了它。我的应用程序 build.gradle 如下所示:

repositories {
    jcenter()
}

apply plugin: 'com.android.application'

android {
    ...
    sourceSets {
        androidTest.setRoot('src/test')
    }
}

dependencies {
    ...

    // Testing
    testCompile 'junit:junit:4.12'
    testCompile 'org.easytesting:fest:1.0.16'
    testCompile 'com.squareup:fest-android:1.0.8'
    testCompile('org.robolectric:robolectric:3.0-rc2') {
        exclude group: 'commons-logging', module: 'commons-logging'
        exclude group: 'org.apache.httpcomponents', module: 'httpclient'
    }
}

最后一天我也遇到了 problem.Atlast 我得到了解决方案。

错误是由于您为测试 class 创建了 java 文件夹作为 java 文件夹,该文件夹在 蓝色 中显示=30=] studio.Actually 你需要绿色

为了测试,您只需要创建名称为 java 的文件夹,其他一切都将由 android 工作室自己完成。

要删除 java 文件夹依赖项,您可以删除

  sourceSets {
    androidTest.setRoot('src/test')
}

代码来自 build.gradle

那么一切都会好起来的