Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unmockkAll doesn't work if mockkConstructor was called more than once on the same class #838

Closed
3 tasks done
Chrostoq opened this issue Jul 5, 2022 · 3 comments
Closed
3 tasks done

Comments

@Chrostoq
Copy link
Contributor

Chrostoq commented Jul 5, 2022

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

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

  1. Call mockkConstructor multiple times on the same class and overwrite a function
  2. Call unmockkAll
  3. 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

    }
}
@Raibaz
Copy link
Collaborator

Raibaz commented Jul 8, 2022

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.

@Chrostoq
Copy link
Contributor Author

Chrostoq commented Jul 9, 2022

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.

@Chrostoq
Copy link
Contributor Author

Fixed with PR #870

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants