Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Duplicate class org.hamcrest.BaseDescription found in modules jetified-hamcrest-core-1.3.jar

Android studio 3.6

app/build.gradle:

 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'

in gradle.properties:

android.useAndroidX=true
android.enableJetifier=true

Here my Espresso instrumentation test:

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\"))")
        )
    }

But I get error:

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)
like image 545
Alexei Avatar asked Nov 28 '19 09:11

Alexei


People also ask

What is Hamcrest core 1.3 jar?

hamcrest-core. jar : This was the core API to be used by third-party framework providers. This includes a foundation set of matcher implementations for common operations. This library was used as a dependency for many third-party libraries, including JUnit 4.

What is Hamcrest core for?

Purpose of the Hamcrest matcher framework. Hamcrest is a widely used framework for unit testing in the Java world. Hamcrest target is to make your tests easier to write and read. For this, it provides additional matcher classes which can be used in test for example written with JUnit.

What is Hamcrest in API?

Hamcrest is a framework for writing matcher objects allowing 'match' rules to be defined declaratively. There are a number of situations where matchers are invaluable, such as UI validation or data filtering, but it is in the area of writing flexible tests that matchers are most commonly used.

How do I add Hamcrest to IntelliJ?

How to add hamcrest methods into your project by IntelliJ as shown below. For example, we are writing a JUnit test with hamcrest “Is” method. First, click “assertThat” and then press alt+Enter then click “Static Import Method…” Then click “is” and press alt+enter and select “Static Import Method…”


2 Answers

The exclusion of junit and giving the priority to Hamcrest will disable the oppotunity to perform unit tests with JUnit! That is why you would get an error in Android studio: Cannot resolve '@Before' or '@Test' while doing unit tests. The correct way to go will be replacing the Hamcrest with JUnit!

Place this code to build.gradle on app level:

configurations.all {
    resolutionStrategy.dependencySubstitution {
        substitute module('org.hamcrest:hamcrest-core:1.1') with module('junit:junit:4.10')
    }
}
like image 69
M22 Avatar answered Oct 21 '22 02:10

M22


I think this problem happened when you add a dependency (as your situation Hamcrest and another dependency, library, Jar files, etc... is using Hamcrest too! but with another version.

If you force your Hamcrest dependency in app Gradle like below might solve your problem:

configurations.all {
    resolutionStrategy {
        force 'org.hamcrest:hamcrest-junit:2.0.0.0'
    }
}

After apply if you get the same error try to exclude like this:

configurations { compile.exclude group: "junit", module: "junit" }
like image 3
Sepehr Avatar answered Oct 21 '22 02:10

Sepehr