You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
unmockkAll should be able to clear all mocks
Current Behavior
Calling mockkConstructor multiple times result in unmockkAll not being able to clear it so the actual class constructor can be used again
Failure Information (for bugs)
internalMockkConstructor only saves the cancellation once for each class with the putIfNotThere. But the JvmConstructorMockFactory still adds a ConstructorMockVariant to its stack
Steps to Reproduce
Call mockkConstructor multiple times on the same class and overwrite a function
Call unmockkAll
Verify if the function is still mocked
Failure Logs
expected: <10> but was: <30>
Expected :10
Actual :30
Stack trace
org.opentest4j.AssertionFailedError: expected: <10> but was: <30>
at app//org.junit.jupiter.api.AssertionUtils.fail(AssertionUtils.java:55)
at app//org.junit.jupiter.api.AssertionUtils.failNotEqual(AssertionUtils.java:62)
at app//org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:182)
at app//org.junit.jupiter.api.Assertions.assertEquals(Assertions.java:1152)
at app//kotlin.test.junit5.JUnit5Asserter.assertEquals(JUnitSupport.kt:32)
at app//kotlin.test.AssertionsKt__AssertionsKt.assertEquals(Assertions.kt:63)
at app//kotlin.test.AssertionsKt.assertEquals(Unknown Source)
at app//kotlin.test.AssertionsKt__AssertionsKt.assertEquals$default(Assertions.kt:62)
at app//kotlin.test.AssertionsKt.assertEquals$default(Unknown Source)
at app//com.example.myapplication.ExampleUnitTest.testMocking(ExampleUnitTest.kt:32)
at java.base@11.0.12/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
Minimal reproducible code (the gist of this issue)
dependencies {
testImplementation(platform('org.junit:junit-bom:5.8.2'))
testImplementation('org.junit.jupiter:junit-jupiter')
testImplementation "io.mockk:mockk:1.12.4"
testImplementation 'org.jetbrains.kotlin:kotlin-test'
}
// -----------------------[ CODE ] -----------------------
package com.example.myapplication
import io.mockk.every
import io.mockk.mockkConstructor
import io.mockk.unmockkAll
import io.mockk.unmockkConstructor
import org.junit.jupiter.api.Test
import kotlin.test.assertEquals
class ExampleClass {
val x = 10
}
class ExampleUnitTest {
@Test
fun testMocking() {
assertEquals( 10, ExampleClass().x) // works
mockkConstructor(ExampleClass::class)
every { anyConstructed<ExampleClass>().x } returns 20
assertEquals( 20, ExampleClass().x) // works
mockkConstructor(ExampleClass::class)
every { anyConstructed<ExampleClass>().x } returns 30
assertEquals( 30, ExampleClass().x) // works
unmockkAll()
assertEquals( 10, ExampleClass().x) // fails
unmockkAll()
assertEquals( 10, ExampleClass().x) // still fails
unmockkConstructor(ExampleClass::class)
assertEquals( 10, ExampleClass().x) // works
}
}
The text was updated successfully, but these errors were encountered:
This is indeed an unexpected behavior, but I would also add that calling mockkConstructor more than once in a single test looks a bit like a code smell to me.
However, you're right in saying that unmockkAll() should be clearing all existing ConstructorMockVariants.
Shouldn't be too hard to put together a PR about this, I'd be glad to review it.
For ~1300 tests it wasn't a problem, but the one time it was done it created a sporadic fail on a different test (depending on the order gradle decided to run the tests in) which was not that easy to find.
I might be able to find some time to do a PR, but not sure yet.
Prerequisites
Please answer the following questions for yourself before submitting an issue.
Expected Behavior
unmockkAll should be able to clear all mocks
Current Behavior
Calling mockkConstructor multiple times result in unmockkAll not being able to clear it so the actual class constructor can be used again
Failure Information (for bugs)
internalMockkConstructor
only saves the cancellation once for each class with theputIfNotThere
. But theJvmConstructorMockFactory
still adds aConstructorMockVariant
to its stackSteps to Reproduce
Failure Logs
expected: <10> but was: <30>
Expected :10
Actual :30
Stack trace
Minimal reproducible code (the gist of this issue)
The text was updated successfully, but these errors were encountered: