【问题标题】:How to use Mockito/Hamcrest in unit tests in Android Studio如何在 Android Studio 的单元测试中使用 Mockito/Hamcrest
【发布时间】:2014-08-18 16:49:11
【问题描述】:

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

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

  • 使用 gradle 构建
  • 使用官方 Android API 进行测试(ActivityInstrumentationTestCase2 等)
  • 将测试放在应用的目录中,而不是作为单独的模块
  • 在 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')
    }
}

【问题讨论】:

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


    【解决方案1】:

    我通过使用“依赖项”下的“androidTestCompile”选项使其正常工作,如here 所述。

    我所做的是:

    • 创建了一个名为 libs-tests 的文件夹,其中包含仅应用于测试的 jar。
    • 将该文件夹添加为使用“androidTestCompile”进行测试的依赖项

    现在,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')
        }
    }
    

    【讨论】:

      【解决方案2】:

      我也有同样的问题。

      1. 我用androidTestCompile 代替testCompile
      2. 我还必须拥有classpath 'com.android.tools.build:gradle:1.1.0' [或更高]
      3. 然后我在执行 sync 之前执行了 buildDependecies

      这就是我为消除此错误所做的一切。

      【讨论】:

        【解决方案3】:

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

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

        【讨论】:

          【解决方案4】:

          我使用 androidTestCompile 而不是 testCompile

          【讨论】:

            猜你喜欢
            • 2016-09-02
            • 2017-08-27
            • 1970-01-01
            • 2020-02-22
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多