首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >模块中发现的重复类org.hamcrest.BaseDescription -hamcrest-core-1.3.jar

模块中发现的重复类org.hamcrest.BaseDescription -hamcrest-core-1.3.jar
EN

Stack Overflow用户
提问于 2019-11-28 09:25:43
回答 2查看 5.7K关注 0票数 7

Android演播室3.6

应用程序/build.gradle:

代码语言:javascript
复制
 androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'com.azimolabs.conditionwatcher:conditionwatcher:0.2'
    // Espresso framework
    androidTestImplementation "androidx.test.espresso:espresso-core:$espresso_version"
    androidTestImplementation "androidx.test.espresso:espresso-intents:$espresso_version"
    androidTestImplementation "androidx.test.espresso:espresso-contrib:$espresso_version"
    androidTestImplementation 'org.hamcrest:hamcrest-junit:2.0.0.0'

    // UI Automator framework
    androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'
    androidTestImplementation 'com.squareup.okhttp3:mockwebserver:3.8.0'

    // for test fragments
    debugImplementation 'androidx.fragment:fragment-testing:1.2.0-rc02'

    testImplementation 'junit:junit:4.12'
    testImplementation 'com.nhaarman:mockito-kotlin-kt1.1:1.5.0'

在gradle.properties中:

代码语言:javascript
复制
android.useAndroidX=true
android.enableJetifier=true

这里我的Espresso仪器测试:

代码语言:javascript
复制
import okhttp3.mockwebserver.MockWebServer
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.hamcrest.text.MatchesPattern
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class FeedbackActivityTransportTest {
 @Test
    fun buttonSend_click_checkRequest() {
        val request = mockServer.takeRequest();
        assertEquals("POST", request.method)
        assertThat(
            request.body.toString(),
            MatchesPattern.matchesPattern("(\"feedback.*\\\"type\\\":2\"))")
        )
    }

但我错了:

代码语言:javascript
复制
Duplicate class org.hamcrest.BaseDescription found in modules jetified-hamcrest-core-1.3.jar (org.hamcrest:hamcrest-core:1.3) and jetified-java-hamcrest-2.0.0.0.jar (org.hamcrest:java-hamcrest:2.0.0.0)
Duplicate class org.hamcrest.BaseMatcher found in modules jetified-hamcrest-core-1.3.jar (org.hamcrest:hamcrest-core:1.3) and jetified-java-hamcrest-2.0.0.0.jar (org.hamcrest:java-hamcrest:2.0.0.0)
Duplicate class org.hamcrest.Condition found in modules jetified-hamcrest-core-1.3.jar (org.hamcrest:hamcrest-core:1.3) and jetified-java-hamcrest-2.0.0.0.jar (org.hamcrest:java-hamcrest:2.0.0.0)
Duplicate class org.hamcrest.Condition$1 found in modules jetified-hamcrest-core-1.3.jar (org.hamcrest:hamcrest-core:1.3) and jetified-java-hamcrest-2.0.0.0.jar (org.hamcrest:java-hamcrest:2.0.0.0)
EN

回答 2

Stack Overflow用户

发布于 2021-05-17 19:28:05

排除 JUnit 并赋予Hamcrest优先级将禁用使用JUnit执行单元测试的可能性!这就是为什么在执行单元测试时,在Android中会出现一个错误:Cannot resolve '@Before' or '@Test'。正确的方法是将Hamcrest替换为JUnit

将此代码放到build.gradle应用程序级别上:

代码语言:javascript
复制
configurations.all {
    resolutionStrategy.dependencySubstitution {
        substitute module('org.hamcrest:hamcrest-core:1.1') with module('junit:junit:4.10')
    }
}
票数 5
EN

Stack Overflow用户

发布于 2019-11-28 11:26:51

我认为当您添加一个依赖项(如Hamcrest和另一个依赖项、库、Jar文件等)时,会出现这个问题。也在使用Hamcrest!但用另一种版本。

如果在应用程序中强制使用Hamcrest依赖关系,那么像下面这样的可能会解决您的问题:

代码语言:javascript
复制
configurations.all {
    resolutionStrategy {
        force 'org.hamcrest:hamcrest-junit:2.0.0.0'
    }
}

应用后,如果得到相同的错误,请尝试排除,如下所示:

代码语言:javascript
复制
configurations { compile.exclude group: "junit", module: "junit" }
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59085448

复制
相关文章

相似问题

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