首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >配置gradle使用JUnit、Mockito、Hamcrest和Espresso

配置gradle使用JUnit、Mockito、Hamcrest和Espresso
EN

Stack Overflow用户
提问于 2014-11-05 20:24:28
回答 2查看 14.4K关注 0票数 8

我有一个Android项目,它使用Mockito、Hamcrest和Espresso来帮助测试。

无论我对Gradle构建文件做了什么尝试,当我尝试在执行NoSuchMethodError之后运行测试时,我都会为org.hamcrest.Matcher.anyOf获得一个gradle androidTestCompile

以下是我的当前配置:

代码语言:javascript
复制
dependencies {
    compile project(':GameCore')
    androidTestCompile files(
            'libs/espresso-1.1.jar',
            'libs/testrunner-1.1.jar',
            'libs/testrunner-runtime-1.1.jar'
    )
    androidTestCompile(
            'junit:junit:4.11',
            'org.mockito:mockito-core:1.10.0',
            'com.google.guava:guava:14.0.1',
            'com.squareup.dagger:dagger:1.1.0',
            'com.google.dexmaker:dexmaker:1.0',
            'com.google.dexmaker:dexmaker-mockito:1.0',
            'org.hamcrest:hamcrest-core:1.3',
            'org.hamcrest:hamcrest-library:1.3'
            )

我尝试重写Mockito和JUnit要求,以排除Hamcrest,如下所示:

代码语言:javascript
复制
androidTestCompile('junit:junit:4.11') {
    exclude group: 'org.hamcrest'
}

但这没什么区别。

GameCore项目是一个纯Java项目。它还依赖于JUnit和Mockito,但由于它们被指定为testCompile,我认为它们不应该干扰。

这个模块用于“androidTestCompile`”的gradle dependencies输出是:

代码语言:javascript
复制
+--- junit:junit:4.11                                                               
|    \--- org.hamcrest:hamcrest-core:1.3
+--- org.mockito:mockito-core:1.10.0
|    +--- org.hamcrest:hamcrest-core:1.1 -> 1.3
|    \--- org.objenesis:objenesis:2.1
+--- com.google.guava:guava:14.0.1
+--- com.squareup.dagger:dagger:1.1.0
|    \--- javax.inject:javax.inject:1
+--- com.google.dexmaker:dexmaker:1.0
+--- com.google.dexmaker:dexmaker-mockito:1.0
|    +--- com.google.dexmaker:dexmaker:1.0
|    \--- org.mockito:mockito-core:1.9.5 -> 1.10.0 (*)
+--- org.hamcrest:hamcrest-core:1.3
\--- org.hamcrest:hamcrest-library:1.3
     \--- org.hamcrest:hamcrest-core:1.3

编辑

在进一步研究了这个问题之后,我发现浓缩咖啡需要Hamcrest 1.1,但我也使用了assertThat,它在Hamcrest 1.3中。Hamcrest 1.3没有espresso使用的anyOf方法。所以我想我被困住了

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-11-06 00:45:00

我意识到assertThat在Hamcrest 1.1中,它只是在hamcrest-integration而不是hamcrest-core。我更改了我的构建文件,现在都在工作了:

代码语言:javascript
复制
androidTestCompile files(
        'libs/espresso-1.1.jar',
        'libs/testrunner-1.1.jar',
        'libs/testrunner-runtime-1.1.jar'
)
androidTestCompile(
        'org.mockito:mockito-core:1.9.5',
        'com.google.dexmaker:dexmaker-mockito:1.0',
        'com.google.dexmaker:dexmaker:1.0',
        'com.google.guava:guava:14.0.1',
        'com.squareup.dagger:dagger:1.1.0',
        'org.hamcrest:hamcrest-core:1.1',
        'org.hamcrest:hamcrest-integration:1.1',
        'org.hamcrest:hamcrest-library:1.1'
)

我尝试使用espresso-1.1-bundled.jar,但是这导致了dex错误,因为两个Hamcrest 1.1的副本被拉了进来,所以我不得不将它从一堆依赖项中排除出来。

票数 5
EN

Stack Overflow用户

发布于 2014-11-05 23:15:30

目前,我正在我的Android项目中使用所有这些库(我非常喜欢测试和TDD :)。

下面是我的build.gradle文件的依赖项部分:

代码语言:javascript
复制
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    //noinspection GradleDependency
    compile 'com.google.android.gms:play-services:5.0.89'
    provided 'com.squareup.dagger:dagger-compiler:1.2.1'
    compile 'com.jakewharton:butterknife:5.1.2'
    compile 'com.squareup.dagger:dagger:1.2.1'
    compile 'com.squareup.okhttp:okhttp-urlconnection:2.0.0'
    compile 'com.squareup.okhttp:okhttp:2.0.0'
    compile 'com.squareup.retrofit:retrofit:1.6.0'
    compile 'com.google.code.gson:gson:2.3'
    compile 'com.squareup:otto:1.3.5'
    compile 'javax.annotation:javax.annotation-api:1.2'
    compile 'com.google.code.findbugs:jsr305:1.3.9'
    compile 'com.j256.ormlite:ormlite-android:4.43'
    compile 'com.j256.ormlite:ormlite-core:4.43'
    compile 'com.android.support:support-v13:20.0.0'
    compile 'com.path:android-priority-jobqueue:1.1.2'
    compile'com.squareup.picasso:picasso:2.3.3'
    compile 'com.github.johnkil.android-robototextview:robototextview:2.1.0'
    compile 'se.emilsjolander:stickylistheaders:2.5.0'
    compile 'com.newrelic.agent.android:android-agent:4.+'

    compile 'com.github.chrisbanes.actionbarpulltorefresh:library:0.9.9'

    //mockito dependencies
    androidTestCompile 'org.mockito:mockito-core:1.9.5'
    androidTestCompile files('libs/dexmaker-mockito-1.0.jar')
    androidTestCompile files('libs/dexmaker-1.0.jar')

    //espresso dependencies
    androidTestCompile 'com.google.guava:guava:18.0'
    androidTestCompile 'com.squareup.spoon:spoon-client:1.1.1'
    androidTestCompile('com.jakewharton.espresso:espresso:1.1-r3') {
        exclude group: 'com.squareup.dagger'
    }
    compile('com.crashlytics.sdk.android:crashlytics:2.0.0@aar') {
        transitive = true;
    }
}

您可能会忽略其中的许多,因此下面列出了要测试的特定列表:

代码语言:javascript
复制
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile project(':facebook')
    compile project(':library')
    //noinspection GradleDependency

    //mockito dependencies
    androidTestCompile 'org.mockito:mockito-core:1.9.5'
    androidTestCompile files('libs/dexmaker-mockito-1.0.jar')
    androidTestCompile files('libs/dexmaker-1.0.jar')

    //espresso dependencies
    androidTestCompile 'com.google.guava:guava:18.0'
    androidTestCompile 'com.squareup.spoon:spoon-client:1.1.1'
    androidTestCompile('com.jakewharton.espresso:espresso:1.1-r3') 
}

在这里可以找到右旋者的利布斯:https://code.google.com/p/dexmaker/downloads/detail?name=dexmaker-1.0.jar&can=2&q=

在这里:

https://code.google.com/p/dexmaker/downloads/detail?name=dexmaker-mockito-1.0.jar&can=2&q=

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26766433

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档