android - 如何修复截击测试编译

标签 android junit android-volley

我在项目中使用谷歌的 volley 库。我将它添加为一个模块,但在编译过程中出现以下错误。

Error

这是目录结构:

dir_structure

我尝试添加

// testing
testCompile 'org.robolectric:robolectric:2.4'
testCompile 'junit:junit:4.+'

到应用程序的 build.gradle,但这没有帮助。

所以显而易见的问题是如何让它发挥作用?

更新:

Volley 的 build.gradle

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

        // testing
        testCompile 'org.robolectric:robolectric:2.4'
        testCompile 'junit:junit:4.+'
    }
}

apply plugin: 'com.android.library'

android {
    compileSdkVersion 19
    buildToolsVersion = '21.1.0'
}

apply from: 'rules.gradle'

应用的build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "19.1.0"

    defaultConfig {
        applicationId "com.example.vjdhama.project"
        minSdkVersion 15
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'),     'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.0.0'

    compile project(":volley")
}

错误:

/home/vjdhama/Documents/Coding/Android/project/volley/build.gradle
Error:Error:line (18)Gradle DSL method not found: 'testCompile()'
Possible causes:<ul><li>The project 'project' may be using a version of Gradle that does not contain the method.
<a href="openGradleSettings">Gradle settings</a></li><li>The build file may be missing a Gradle plugin.
<a href="apply.gradle.plugin">Apply Gradle plugin</a></li>

最佳答案

+1 同样的问题。我似乎仍然可以在我更大的 android 项目中使用该库,但测试不会运行。似乎最新版本的 Volley 已经破坏了 build.gradle 中的依赖关系。不是答案,因为它似乎不起作用,但也许你会比我幸运。

通过添加

dependencies {
    testCompile 'org.robolectric:robolectric:2.3'
    testCompile 'junit:junit:4.11'
    testCompile 'org.mockito:mockito-core:1.9.5'
}

到 volley 的 build.gradle 文件的底部,我可以让运行使所有测试失败而不是使构建失败。我猜是这样。

来自 volley 的完整 build.gradle:

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

apply plugin: 'com.android.library'

android {
    compileSdkVersion 22
    buildToolsVersion = '22.0.1'
}

apply from: 'rules.gradle'

dependencies {
    testCompile 'org.robolectric:robolectric:2.3'
    testCompile 'junit:junit:4.11'
    testCompile 'org.mockito:mockito-core:1.9.5'
}

关于android - 如何修复截击测试编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29433835/

相关文章:

java - Fragment、Volley 和 RecyclerView JSON

android - 游戏代理 : Volley error when reporting played

android-studio - Gradle说由于 Volley 库,构建失败

java - Android位图 - 将圆裁剪成半圆

php - 将 json 解析为字符串时出错

android - 如何在按下按钮 Home i Back 时完成应用程序?

junit - Kotlin for assertThat(foo, instanceOf(Bar.class))

android - RxJava2 与 JUnit : no exceptions thrown in tests

android - 如何让我的 ListView 项目具有(并保持)不同的背景?

java - 运行 ant junit 时出现 ClassNotFoundException