2014-11-05 46 views
8

我有一个使用Mockito,Hamcrest和Espresso帮助测试的Android项目。配置gradle使用JUnit,Mockito,Hamcrest和Espresso

无论我尝试使用Gradle构建文件,在gradle androidTestCompile之后尝试运行我的测试时,我得到NoSuchMethodErrororg.hamcrest.Matcher.anyOf

这里是我当前的配置:

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像这样:

androidTestCompile('junit:junit:4.11') { 
    exclude group: 'org.hamcrest' 
} 

但是这并没有什么差别。

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

此模块“androidTestCompile`的输出gradle dependencies是:

+--- 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方法。所以我想我被卡住:)

+0

你可以在该模块上运行'gradle dependencies'并发布相关任务的输出吗? – 2014-11-05 22:34:05

回答

4

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

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的两个副本都在拉,所以我不得不从一堆排除依赖。

+0

太棒了!我很高兴你的工作。我个人更喜欢杰克沃顿的捆绑咖啡包,但每个人都喜欢。快乐的测试! – 2014-11-06 11:53:04

1

我目前在我的Android Gradle项目中使用所有这些库(我真的很喜欢测试和TDD以及:))。

这里是我的build.gradle文件的相关部分:

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:[email protected]') { 
     transitive = true; 
    } 
} 

很多这些你大概可以忽略所以这里只指定具体的测试清单:

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

而且dexmaker库可以在这里找到: 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=

+0

感谢您发布您的构建文件,但我没有在这里看到hamcrest。它在你的libs目录中吗? – Pikaling 2014-11-05 23:22:36

+0

Hamcrest带有浓咖啡,​​所以我希望它包含在那个库里。 – 2014-11-05 23:23:51

+1

好的。我没有使用一体式版本的浓缩咖啡,因为我试图避免依赖冲突,因此我单独包含了hamcrest – Pikaling 2014-11-05 23:28:51

相关问题