question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"Duplicate class io.mockk.ValueClassSupportKt found" when using in 1.12.0

See original GitHub issue

Prerequisites

Please answer the following questions for yourself before submitting an issue.

  • I am running the latest version
  • I checked the documentation and found no answer
  • I checked to make sure that this issue has not already been filed

Expected Behaviour

Android instrumented tests should run.

Current Behaviour

Android instrumented tests fail with Duplicate class io.mockk.ValueClassSupportKt found compilation error (see stack trace below).

Failure Information (for bugs)

When upgrading from 1.11.0 to 1.12.0, tests no longer run. Seems to be affecting others that have commented on #653.

Steps to Reproduce

Please provide detailed steps for reproducing the issue.

  1. Run tests successfully
  2. Upgrade Gradle dependencies to 1.12.0
  3. Tests fail

Context

Please provide any relevant information about your setup. This is important in case the issue is not reproducible except for under certain conditions.

  • MockK version: 1.12.0
  • OS: macOS 12
  • Kotlin version: 1.53.1
  • JDK version: 11
  • JUnit version: 5
  • Type of test: android instrumented test

Stack trace

> Task :app:checkDebugAndroidTestDuplicateClasses FAILED
Execution failed for task ':app:checkDebugAndroidTestDuplicateClasses'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.CheckDuplicatesRunnable
   > Duplicate class io.mockk.ValueClassSupportKt found in modules jetified-mockk-agent-android-1.12.0-runtime (io.mockk:mockk-agent-android:1.12.0) and jetified-mockk-agent-jvm-1.12.0 (io.mockk:mockk-agent-jvm:1.12.0)

Minimal reproducible code (the gist of this issue)

dependencies {
	...
    testImplementation "io.mockk:mockk:1.12.0"
    testImplementation "io.mockk:mockk-agent-jvm:1.12.0"
	...
	androidTestImplementation "io.mockk:mockk-android:1.12.0"
    androidTestImplementation "io.mockk:mockk-agent-jvm:1.12.0"
}
@Test
fun noPermission() {
    ActivityScenario.launch(MainActivity::class.java).use {
        onView(withId(R.id.has_permission))
            .check(matches(withText("No permission.")))
    }
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:12
  • Comments:5

github_iconTop GitHub Comments

13reactions
HarisHouliscommented, Oct 21, 2021

What worked for me was excluding the second module for the androidTestImplementation configuration:

    configurations {
        androidTestImplementation {
            exclude group: 'io.mockk', module: 'mockk-agent-jvm'
        }
    }
0reactions
azabostcommented, Mar 24, 2022

What worked for me was excluding the second module for the androidTestImplementation configuration:

    configurations {
        androidTestImplementation {
            exclude group: 'io.mockk', module: 'mockk-agent-jvm'
        }
    }

Any idea how to apply this to all the modules at once?

When I tried this in the top-level (root) build.gradle.kts:

allprojects {
    configurations.findByName("androidTestImplementation")?.run {
        logger.log(LogLevel.LIFECYCLE, "Excluding mockk-agent-jvm from: $projectName")
        exclude(group = "io.mockk", module = "mockk-agent-jvm")
    } ?: run {
        logger.log(LogLevel.LIFECYCLE, "Skipping exclusion of mockk-agent-jvm from: $projectName")
    }
}

then it didn’t work. It looks like at this stage the configurations are not prepared yet so there is no way to find androidTestImplementation.

The suggested workaround worked for me only when I added it in the module-specific build.gradle.kts file but I don’t want to add it to all of my modules because it’s really tedious and easy to forget about, not to mention updating this in the future if necessary.

Do you really think this issue should be closed already? Is there no way for the mockk library to fix this problem?


EDIT (Mar 24 14:44 UTC):

If anyone is interested, I figured out I can exclude mockk-agent-jvm from all the modules at once either by something like:

allprojects {
    afterEvaluate {
        configurations.findByName("androidTestImplementation")?.run { 
            exclude(group = "io.mockk", module = "mockk-agent-jvm")
        }
    }
}

or by extracting the common parts of the module’s setup from the android { ... } block from each build.gradle.kts into buildSrc and handling it there, for example something like:

fun Project.removeMockkAgentJvmFromAndroidTests() {
    configurations.findByName("androidTestImplementation")?.run {
        exclude(group = "io.mockk", module = "mockk-agent-jvm")
    }
}

fun Project.defaultAndroidLibrary() = android {
    removeMockkAgentJvmFromAndroidTests()
}

// Then, in each module's build.gradle.kts:

defaultAndroidLibrary()
Read more comments on GitHub >

github_iconTop Results From Across the Web

Android Kotlin Unit test failing with io.mockk.MockKException
In your setup() method, you're creating a viewmodel first and then configuring your mock reponse in the repository after.
Read more >
MockK | mocking library for Kotlin
Provides DSL to mock behavior. Built from zero to fit Kotlin language. Supports named parameters, object mocks, coroutines and extension function mocking.
Read more >
io.mockk : mockk : 1.12.0 - Maven Central Repository Search
MockK - mocking library for Kotlin.
Read more >
Mockk for Java Mockito developers, an introduction - Medium
All the code in this blog post can be found in this Github repo. Getting started. The only requirement to use both testing...
Read more >
MockK: A Mocking Library for Kotlin - Baeldung
Most JVM mock libraries have problems with mocking or stubbing final classes. Of course, we can add the “open” keyword to classes and ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found