测试错误 - NoClassDefFoundError:解析失败:Lorg/hamcrest/Matchers

问题描述 投票:0回答:4

我正在使用 Espresso 进行仪器测试,但在堆栈跟踪上出现此错误:

enter image description here

由于缺少类而导致的错误,如下所示:

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

在我在片段测试中添加此代码后,它立即发生:

enter image description here

这些是我在 Gradle 上的相关库:

enter image description here

我有这些进口产品:

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
android gradle dependencies android-espresso android-testing
4个回答
28
投票

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

我们可以使用两个版本级别的依赖项运行它两次,并可以检查差异。

  • 多次请求 hamcrest 库版本 1.3,它与大多数常见的 Android 依赖项相匹配。
  • 在左侧(v3.3.0 的依赖树)每个 hamcrest 传递依赖项均按要求提供,毫不奇怪它可以工作!
  • 在右侧(v3.4.0 的依赖树),我们可以看到一些请求在更高版本上“替代”了 hamcrest 库,在这种情况下,这不起作用。

[Gradle dependency tree comparison1


16
投票

我没有尝试接受的答案,但有些回复保存了我的a$$,只需添加:

androidTestImplementation "org.hamcrest:hamcrest:2.2"

6
投票

经过一段时间的挣扎,我意识到错误是由

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"

0
投票

更新 30/7/2024: 现在这个错误已经通过仅实现这个库解决了

// To use the androidx.test.espresso
 androidTestImplementation "androidx.test.espresso:espresso-core:3.6.1" 

https://developer.android.com/training/testing/espresso

© www.soinside.com 2019 - 2024. All rights reserved.