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.

mockkStatic is not threadsafe

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 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 ] -----------------------

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
tg-dclarkecommented, May 10, 2022

Can this be reopened? We’re also having issues with running mockkStatic in parallel

0reactions
nilajaspcommented, Aug 8, 2022

Facing similar issue with mockkObject as well.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to test if static method is thread safe - java - Stack Overflow
It's not thread safe, nor does it attempt to be. A test cannot prove code is thread safe. It can only attempt to...
Read more >
Mock singleton objects and static methods | MockK Guidebook
mockkStatic(Writer::class). Like object mocks, static mocks behave like spies. The real method will be called if the method is not stubbed.
Read more >
How to applied collect Method from Observable Java to Kotlin ...
How to mock static java method from kotlin , mockkStatic not working · How to convert this sorted map method from ... Are...
Read more >
Static method call not being mocked on some threads using ...
I am trying to unit test some code that is using RxJava for asynchronicity and parallelism and that is using Realm database to...
Read more >
download file - DOKUMEN.PUB
A must-read book, not just to get up and running with Kotlin but also ... to constructors, ensure thread safety, and, at the...
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