mockkStatic is not threadsafe
See original GitHub issuePrerequisites
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.
- Write two test cases which use mockkStatic on the same class.
- Execute test cases in JUnit parallel execution mode.
- 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:
- Created 2 years ago
- Reactions:1
- Comments:5
Top GitHub Comments
Can this be reopened? We’re also having issues with running
mockkStatic
in parallelFacing similar issue with mockkObject as well.