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

2024-03-09

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

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

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

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

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

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

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

但是运行时我得到:

构建脚本错误,发现不支持的 Gradle DSL 方法: '测试编译()'!

虽然我不确定它是否相关,但我使用的 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')
    }
}

我通过使用“依赖项”下的“androidTestCompile”选项使其工作,如所解释的here http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Testing.

我所做的是:

  • 创建了一个名为 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')
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐

  • OAuthException (#368) 尝试的操作已被视为滥用或被禁止

    我正在尝试使用 Graph API 在我的墙上或我的一些朋友的墙上发布提要 我授予了该应用程序所需的所有权限 当我从页面发出请求时允许它们 我拥有有效的访问令牌 但即使发生此异常并且没有发布提要 我的帖子请求看起来不错 已授予权限 我需要做
  • 如何在 jQuery 中选择从当前元素开始的下一个“n”元素?

    如何选择从当前元素开始的下一个 n 元素 我的意思是 this attr 我想做 n 次 以n 4为例 this attr this next attr this next next attr this next next next att
  • 回流类型密封时的满射性检查

    当密封类型上的模式匹配不详尽时 Scala 会发出警告 但是当返回类型被密封时 我们是否可以检查函数是否返回所有情况 例如 考虑以下 ADT sealed trait Foo case object Bar extends Foo case
  • 捆绑包标识符 iOS 配置门户

    我是 iOS 企业计划的团队代理 我正在尝试添加一个新的应用程序 ID 新的应用程序 ID 以前从未在我的帐户中使用过 但可能已被另一个企业帐户使用 但是 提交后 它说 The bundle identifier you have spec
  • 通过 Django 管理站点添加数据时更改大小写(大写/小写)

    我正在配置我的新项目的管理站点 我有点怀疑我应该怎么做 通过管理站点添加数据时点击 保存 所有内容都转换为大写 编辑 好的 我知道 upper 属性 并且我做了一个视图 我知道该怎么做 但我想知道管理站点上是否有任何可用于字段配置的属性 P
  • 针对 Windows Phone 7 Internet Explorer 9 的条件注释

    Problem 条件注释 例如 p All other browsers p 不工作在 Windows Phone 7 上 或者 至少不是我的 Question 有谁知道如何使用这些评论 并且有测试了他们前 WP7 上的 IE 9 是否支持
  • Java Socket 编程不适用于 10,000 个客户端

    我可以创建多个线程来支持套接字编程中的多客户端功能 工作正常 但如果有 10 000 个客户端想要连接 我的服务器就无法创建这么多线程 如何管理线程以便我可以同时监听所有这些客户端 另外 如果在这种情况下服务器想要向特定客户端发送某些内容
  • 是否可以使用 .NET Remoting + TLS 1.2(或 1.1)?

    最近我们的 PCI DSS 扫描失败 并要求我们禁用 TLS 1 0 并启用 TLS 1 1 或 1 2 我在 Windows Server 2008 R2 盒子上找到了如何执行此操作的说明 但我们有一个使用 NET 远程处理的旧应用程序
  • ActiveSupport::Memoizes 指的是哪种 Ruby memoize 模式?

    因此在 Rails 3 2 中 ActiveSupport Memoizes 已被弃用 消息内容如下 DEPRECATION WARNING ActiveSupport Memoizable is deprecated and will b
  • 如何使用 Python OpenCV 优化圆检测?

    我看过几页关于在 python 中使用 opencv 优化圆检测的页面 所有这些似乎都针对特定图片的具体情况 cv2 HoughCircles 的每个参数的起点是什么 由于我不确定推荐值是什么 因此我尝试循环范围 但这并没有产生任何有希望的
  • 我如何在 ubuntu 中将 PDT 与 eclipse 关联/使用?

    我想使用 PDT 和 eclipse 来调试 PHP 我使用的是ubuntu 9 04 谁能帮我 如果可能 请提供详细信息 考虑到这个线程 http dev eclipse org mhonarc lists pdt dev msg0044
  • AngularJS - 可拖动和多个连接的可排序(jQuery UI + Angular-Common)

    我正在尝试延长角度共同 https github com michaeljcalkins angular common非常好拖放模块 https github com michaeljcalkins angular common blob
  • 为什么谓词下推不起作用?

    程序草图 我创建一个 HiveContexthiveContext 有了这个背景 我创建了一个 DataFramedf来自 JDBC 关系表 我注册数据框df via df registerTempTable TESTTABLE 我通过启动
  • Umbraco 检查 - 查询问题

    我使用 检查 检索的对象具有以下数据值 图片链接 https i stack imgur com 8sUOb jpg为了更方便查看 Icon icon shopping basket alt 2 color red IndexType co
  • Spring Security:没有 WebSecurityConfigurerAdapter 的全局 AuthenticationManager

    我试图摆脱 WebSecurityConfigurerAdapter AuthenticationManager 的配置如下 Configuration EnableGlobalMethodSecurity prePostEnabled t
  • pip install t sne 不起作用

    我无法在 Windows 计算机上安装 tsne 软件包 我按照说明进行操作here https github com danielfrg tsne blob master README md安装 Python 的 tsne 包 但要么pi
  • XMLHttpRequest (Ajax) 错误

    我在用着XMLHttpRequest在 JavaScript 中 但是 它给了我一个错误 我不知道我的问题是什么 我必须解析 XML 文件并将其内容分配给网页 这是我的代码
  • 用于将数据复制并粘贴到另一个工作表的宏

    我发现下面的代码可以根据唯一标识符将数据从一个工作表复制并粘贴到另一个工作表 它还将工作表重命名为唯一标识符 除了公式现在显示为数字之外 它的工作效果非常好 请有人知道如何修改代码以便保留实际公式 Dim wsAll As Workshee
  • 如何在CSS中设置列表的宽度

    我有代码 如下所示 文本不对齐有点烦人 它会影响其他li元素 我怎样才能使它成为固定宽度 我尝试过 width XYZpx 但没有任何作用 li class date 2 28 2010 9 37 38 AM li li class dat
  • 如何在 Android Studio 的单元测试中使用 Mockito/Hamcrest

    我希望能够在 Android Studio 中进行单元测试和仪器测试 并在其中使用 Mockito 我正在 Android Studio 0 8 中使用新方法进行测试 这是 使用 gradle 构建 使用官方Android API进行测试