如何使用的Mockito / Hamcrest在机器人工作室的单元测试 [英] How to use Mockito/Hamcrest in unit tests in Android Studio

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

问题描述

我希望能够做单元测试和仪器测试,Android的工作室,并在他们使用的Mockito。

我使用的测试,机器人工作室0.8的新方法。这就是:

  • 建筑摇篮
  • 在使用Android官方API进行测试(ActivityInstrumentationTestCase2等)
  • 在其应用程序的目录下的测试,而不是作为一个单独的模块
  • 在推出Android中Studio中的测试为Android的测试运行配置

如何写code在我的测试依赖于仅用于测试中使用的库,例如的Mockito或hamcrest?

我想编译和运行测试时,包括这些库,但要避免他们被出口到发布的.apk文件。

的https://$c$c.google.com/p /的Mockito /维基/ DeclaringMockitoDependency 我读过,我要补充的依赖关系为:

 相关性{
    ....
    testCompileorg.mockito:的Mockito核心:1.9.5
}
 

但是,当运行我得到:

  

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

虽然我不知道这是相关的,我使用的摇篮构建文件是:

 应用插件:'机器人'

依赖{
    编译文件树(导演:库,包括:的* .jar)
    编制项目(:Android的SDK)
    testCompileorg.mockito:的Mockito核心:1.9.5
}

安卓{
    compileSdkVersion 19
    buildToolsVersion20.0.0

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }

    sourceSets {
        主要 {
            manifest.srcFile的Andr​​oidManifest.xml
            java.srcDirs = ['src'中]
            resources.srcDirs = ['src'中]
            aidl.srcDirs = ['src'中]
            renderscript.srcDirs = ['src'中]
            res.srcDirs = ['水库']
            assets.srcDirs = ['资产']
        }

        //移动测试,测试/ java中,测试/ RES等...
        androidTest.setRoot(测试)

        //注 - 运行命令行测试:
        // $摇篮干净connectedCheck构建
        //(需要摇篮1.10)

        //将建设各类建设类型/<类型>
        //例如,构建类型/调试/ java中,构建类型/调试/ AndroidManifest.xml中,...
        //这会将他们从src下他们默认位置/<类型> / ...这将
        //与SRC /冲突正在使用的主要来源集中。
        //添加新的构建类型或产品的口味应附有
        //通过类似定制。
        debug.setRoot(集结类型/调试)
        release.setRoot(集结类型/释放)
    }
}
 

解决方案

我把它用在相关性的androidTestCompile选项的工作,作为解释的这里

我所做的是:

  • 创建了一个名为库检验与应该只用于测试的罐子文件夹。
  • 在加入该文件夹的一个依赖关系与androidTestCompile
  • 测试

现在的摇篮构建文件表示为:

 应用插件:'机器人'

依赖{
    编制项目(:Android的SDK)

    // libs文件夹包含在真正的应用程序的APK
    编译文件树(导演:库,包括:的* .jar)

    //测试-libs文件夹包括它只是为了测试
    androidTestCompile文件树(导演:库检验,包​​括:的* .jar)
}


安卓{
    compileSdkVersion 19
    buildToolsVersion20.0.0

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }


    sourceSets {
        主要 {
            manifest.srcFile的Andr​​oidManifest.xml
            java.srcDirs = ['src'中]
            resources.srcDirs = ['src'中]
            aidl.srcDirs = ['src'中]
            renderscript.srcDirs = ['src'中]
            res.srcDirs = ['水库']
            assets.srcDirs = ['资产']
        }

        //移动测试,测试/ java中,测试/ RES等...
        androidTest.setRoot(测试)

        //注 - 运行命令行测试:
        // $摇篮干净connectedCheck构建
        //(需要摇篮1.10)

        //将建设各类建设类型/<类型>
        //例如,构建类型/调试/ java中,构建类型/调试/ AndroidManifest.xml中,...
        //这会将他们从src下他们默认位置/<类型> / ...这将
        //与SRC /冲突正在使用的主要来源集中。
        //添加新的构建类型或产品的口味应附有
        //通过类似定制。
        debug.setRoot(集结类型/调试)
        release.setRoot(集结类型/释放)
    }
}
 

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

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

  • building with gradle
  • using official Android API for testing (ActivityInstrumentationTestCase2, etc)
  • having the tests inside the directory of the app, not as a separate module
  • launching the tests in Android Studio as a "Android Test" run configuration

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

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

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"
}

But when running I get:

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

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

解决方案

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

What I have done is:

  • created a folder called libs-tests with the jars that should only be used for testing.
  • added that folder as a dependency for tests with "androidTestCompile"

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

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

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