如何在 Android Studio 的单元测试中使用 Mockito/Hamcrest [英] How to use Mockito/Hamcrest in unit tests in Android Studio

查看:46
本文介绍了如何在 Android Studio 的单元测试中使用 Mockito/Hamcrest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to be able to make unit tests and instrumentation tests in Android Studio, and using Mockito in them.

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

I'm using the new approach for tests in Android Studio 0.8. This is:

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

如何在测试中编写依赖于仅用于测试的库(例如 mockito 或 hamcrest)的代码?

How can I write code in my tests that depends on libraries used only for the tests, such as mockito or hamcrest?

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

I'd like to include these libraries when compiling and running the tests, but avoid them to be exported to the released .apk.

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

In https://code.google.com/p/mockito/wiki/DeclaringMockitoDependency I've read that I should add the dependency as:

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

但是在运行时我得到:

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

Build script error, Unsupported Gradle DSL method found: 'testCompile()'!

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

Although I'm not sure it's relevant, the gradle build file I'm using is:

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')
    }
}

推荐答案

我通过使用dependencies"下的androidTestCompile"选项使其工作,如这里.

I got it working by using the "androidTestCompile" options under "dependencies", as explained here.

我所做的是:

  • 创建了一个名为 libs-tests 的文件夹,其中包含只应用于测试的 jar.
  • 使用androidTestCompile"将该文件夹添加为测试的依赖项

现在,gradle 构建文件如下:

Now, the gradle build file stands as:

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')
    }
}

这篇关于如何在 Android Studio 的单元测试中使用 Mockito/Hamcrest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆