Skip to content

mockkStatic is not threadsafe #764

@willpxxr

Description

@willpxxr

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 Behavior

Whenever tests are ran in a multithreaded execution, threads executing mockkStatic with the same class should not cancel another threads mockkStatic. ie. mockkStatic should have thread local scope.

Current Behavior

Whenever tests are ran in a multithreaded execution, mockkStatic results in a race condition where multiple threads executing tests may cancel mockkStatic's set by other threads resulting in undefined behavior.

Failure Information (for bugs)

Please help provide information about the failure if this is a bug. If it is not a bug, please remove the rest of this template.

Steps to Reproduce

Please provide detailed steps for reproducing the issue.

  1. Write two test cases which use mockkStatic on the same class.
  2. Execute test cases in JUnit parallel execution mode.
  3. Tests will result in undefined behavior due threads cancelling each others mockkStatic.

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.1
  • OS: Windows 10
  • Kotlin version: 1.6.0
  • JDK version: Azul 11
  • JUnit version: 5.8.2
  • Type of test: Unit test

Minimal reproducible code (the gist of this issue)

See the following github repo: https://github.com/willpxxr/mockkstatic

// -----------------------[ GRADLE DEFINITIONS ] -----------------------
dependencies {
    implementation 'org.jetbrains.kotlin:kotlin-stdlib:1.6.0'
    testImplementation "io.mockk:mockk:1.12.1"
    testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.8.2'
    testImplementation 'org.jetbrains.kotlin:kotlin-test:1.6.0'
}

test {
    useJUnitPlatform()

    systemProperties = [
        'junit.jupiter.execution.parallel.mode.default': 'concurrent',
        'junit.jupiter.execution.parallel.enabled': 'true'
    ]
}
// -----------------------[ YOUR CODE STARTS HERE ] -----------------------
//-----------------------------------[MAIN]------------------------------------
package com.willpxxr

fun staticFoo(): String = "foo"
//-----------------------------------[TEST]-------------------------------------
package com.willpxxr

import io.mockk.every
import io.mockk.mockkStatic
import org.junit.jupiter.api.Test
import kotlin.test.assertEquals

class Test {
    private val testFoo = "bar"


    @Test
    fun `test 1`() {
        mockkStatic(::staticFoo)

        every { staticFoo() } returns testFoo

        assertEquals(
            testFoo,
            staticFoo()
        )
    }

    @Test
    fun `test 2`() {
        mockkStatic(::staticFoo)

        every { staticFoo() } returns testFoo

        assertEquals(
            testFoo,
            staticFoo()
        )
    }
}
// -----------------------[ YOUR CODE ENDS HERE ] -----------------------

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions