我正在使用 Espresso 进行仪器测试,但在堆栈跟踪上出现此错误:
由于缺少类而导致的错误,如下所示:
Caused by: java.lang.ClassNotFoundException: Didn't find class "org.hamcrest.Matchers" on path: DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/system/framework/android.test.mock.jar", zip file "/system/framework/android.test.base.jar", zip file "/data/app/~~vnZzxGNKnS4V6YkEf4falA==/com.example.android.architecture.blueprints.reactive.test-K_x0_yJ0hJeDHaJkDmHXRw==/base.apk", zip file "/data/app/~~oeYx2MgTcILbk-vq_WPx1A==/com.example.android.architecture.blueprints.reactive-0wMHYEe95hx_1cnbdAoZAw==/base.apk"],nativeLibraryDirectories
在我在片段测试中添加此代码后,它立即发生:
这些是我在 Gradle 上的相关库:
我有这些进口产品:
import androidx.fragment.app.testing.launchFragmentInContainer
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.*
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.hamcrest.core.IsNot.not
TLDR 答案,您不需要降级整个浓缩咖啡设置 Hamcrest 版本与传递依赖项存在冲突,可以轻松解决:
如果您正在使用:
androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.4.0'
//change it to:
androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.3.0'
如果您正在使用:
androidTestImplementation 'androidx.test.espresso:espresso-accessibility:3.4.0'
//change it to:
androidTestImplementation "androidx.test.espresso:espresso-accessibility:3.3.0"
您应该仍然能够将 espresso 核心保留在更高的 3.4.0 版本
如果你想理解,这里有更长的解释:
要查看幕后发生了什么,我们可以使用依赖项 gradle 任务在 android studio 终端中打印出依赖关系树:
./gradlew :app:dependencies
我们可以使用两个版本级别的依赖项运行它两次,并可以检查差异。
[
我没有尝试接受的答案,但有些回复保存了我的a$$,只需添加:
androidTestImplementation "org.hamcrest:hamcrest:2.2"
经过一段时间的挣扎,我意识到错误是由
espresso-contib
和 espresso-accessibility
依赖项引起的。我的浓缩咖啡版本是最新的版本 3.4.0-alpha05。
我删除了它们并且测试通过了。
之前,包括
espresso-contrib
和espresso-accessibility
/*core contains matches and view assertions, included by default on android project*/
androidTestImplementation "androidx.test.espresso:espresso-core:$espressoVersion"
//testing code for advanced views such as recyclerview and Date picker
androidTestImplementation "androidx.test.espresso:espresso-contrib:$espressoVersion"
androidTestImplementation "androidx.test.espresso:espresso-accessibility:$espressoVersion"
androidTestImplementation "androidx.test.espresso:espresso-intents:$espressoVersion"
androidTestImplementation "androidx.test.espresso.idling:idling-concurrent:$espressoVersion"
After 注释掉
espresso-contrib
和 espresso-accessibility
//core contains matches and view assertions, included by default on android project
androidTestImplementation "androidx.test.espresso:espresso-core:$espressoVersion"
//testing code for advanced views such as recyclerview and Date picker
//androidTestImplementation "androidx.test.espresso:espresso-contrib:$espressoVersion"
//androidTestImplementation "androidx.test.espresso:espresso-accessibility:$espressoVersion"
androidTestImplementation "androidx.test.espresso:espresso-intents:$espressoVersion"
androidTestImplementation "androidx.test.espresso.idling:idling-concurrent:$espressoVersion"
更新 30/7/2024: 现在这个错误已经通过仅实现这个库解决了
// To use the androidx.test.espresso
androidTestImplementation "androidx.test.espresso:espresso-core:3.6.1"