如何在Android Studio的单元测试中使用Mockito / Hamcrest

时间:2014-08-18 16:49:12

标签: android android-studio automated-tests mockito hamcrest

我希望能够在Android Studio中进行单元测试和测试测试,并在其中使用Mockito。

我在Android Studio 0.8中使用新方法进行测试。这是:

  • 用gradle建造
  • 使用官方Android API进行测试(ActivityInstrumentationTestCase2等)
  • 在app的目录中进行测试,而不是单独的模块
  • 在Android Studio中启动测试,作为" Android测试"运行配置

如何在我的测试中编写代码,这些代码依赖于仅用于测试的库,例如mockito或hamcrest?

我希望在编译和运行测试时包含这些库,但要避免将它们导出到已发布的.apk。

https://code.google.com/p/mockito/wiki/DeclaringMockitoDependency我已经读过,我应该将依赖项添加为:

dependencies {
    ....
    testCompile "org.mockito:mockito-core:1.9.5"
}

但是在跑步的时候我得到了:

  

构建脚本错误,找不到支持的Gradle DSL方法:   ' testCompile()'!

虽然我不确定它是否相关,但我使用的gradle构建文件是:

apply plugin: 'android'

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
    compile project(':android-sdk')
    testCompile "org.mockito:mockito-core:1.9.5"
}

android {
    compileSdkVersion 19
    buildToolsVersion "20.0.0"

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }

        // Move the tests to tests/java, tests/res, etc...
        androidTest.setRoot('tests')

        // Note - to run the tests from command line:
        // $ gradle clean connectedCheck build
        // (requires gradle 1.10)

        // Move the build types to build-types/<type>
        // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
        // This moves them out of them default location under src/<type>/... which would
        // conflict with src/ being used by the main source set.
        // Adding new build types or product flavors should be accompanied
        // by a similar customization.
        debug.setRoot('build-types/debug')
        release.setRoot('build-types/release')
    }
}

4 个答案:

答案 0 :(得分:7)

我使用&#34; androidTestCompile&#34; &#34;依赖关系&#34;下的选项,如here所述。

我所做的是:

  • 使用jars创建了一个名为libs-tests的文件夹,该文件夹只能用于测试。
  • 将该文件夹添加为&#34; androidTestCompile&#34;
  • 的测试依赖项

现在,gradle构建文件代表:

apply plugin: 'android'

dependencies {
    compile project(':android-sdk')

    // The libs folder is included in the apk of the real app
    compile fileTree(dir: 'libs', include: '*.jar')

    // The tests-libs folder is included only for tests
    androidTestCompile fileTree(dir: 'libs-tests', include: '*.jar')
}


android {
    compileSdkVersion 19
    buildToolsVersion "20.0.0"

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }


    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }

        // Move the tests to tests/java, tests/res, etc...
        androidTest.setRoot('tests')

        // Note - to run the tests from command line:
        // $ gradle clean connectedCheck build
        // (requires gradle 1.10)

        // Move the build types to build-types/<type>
        // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
        // This moves them out of them default location under src/<type>/... which would
        // conflict with src/ being used by the main source set.
        // Adding new build types or product flavors should be accompanied
        // by a similar customization.
        debug.setRoot('build-types/debug')
        release.setRoot('build-types/release')
    }
}

答案 1 :(得分:4)

我也遇到了这个问题。

  1. 我使用androidTestCompile代替testCompile
  2. 我还必须拥有classpath 'com.android.tools.build:gradle:1.1.0' [或更高]
  3. 然后我在执行同步之前做了 buildDependecies
  4. 这就是我为此错误所做的一切。

答案 2 :(得分:2)

由于android gradle插件版本1.1.0,支持单元测试,因此您可以在gradle文件中使用testCompile。使用设置&gt;打开它gradle&gt;实验并更新gradle插件版本:

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

答案 3 :(得分:0)

我使用 androidTestCompile 而不是 testCompile