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

我希望能够在

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

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

>用gradle建设

>使用官方Android API进行测试(ActivityInstrumentationTestCase2等)

>将测试放在应用程序的目录中,而不是作为单独的模块

>在Android Studio中将测试作为“Android Test”运行配置启动

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

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

dependencies {

....

testCompile "org.mockito:mockito-core:1.9.5"

}

但是当我跑步时我得到:

Build script error, Unsupported Gradle DSL method found:

‘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/

// For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...

// This moves them out of them default location under src//... 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')

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值