Skip to content

Commit

Permalink
Implement clearStaticMockk for KFunction and KProperty
Browse files Browse the repository at this point in the history
  • Loading branch information
krocard committed Apr 2, 2024
1 parent 143f6bb commit 4917ecc
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
12 changes: 12 additions & 0 deletions modules/mockk/src/jvmMain/kotlin/io/mockk/MockK.kt
Expand Up @@ -44,6 +44,18 @@ fun unmockkStatic(vararg functions: KFunction<*>) =
fun unmockkStatic(vararg functions: KProperty<*>) =
unmockkStatic(*functions.map(KProperty<*>::getter).toTypedArray())

/**
* Clear static mocks.
*/
fun clearStaticMockk(vararg functions: KFunction<*>) =
clearStaticMockk(*functions.map { it.declaringKotlinFile }.toTypedArray())

/**
* Clear static mocks.
*/
fun clearStaticMockk(vararg functions: KProperty<*>) =
clearStaticMockk(*functions.map(KProperty<*>::getter).toTypedArray())

/**
* Builds a static mock and unmocks it after the block has been executed.
*/
Expand Down
27 changes: 27 additions & 0 deletions modules/mockk/src/jvmTest/kotlin/io/mockk/it/StaticMockkTest.kt
@@ -1,5 +1,6 @@
package io.mockk.it

import io.mockk.clearStaticMockk
import io.mockk.every
import io.mockk.mockkStatic
import io.mockk.unmockkStatic
Expand Down Expand Up @@ -73,6 +74,19 @@ class StaticMockkTest {
verify { 5 op 6 }
}

@Test
fun extensionFunctionClearStaticMock() {
mockkStatic(Int::op)

every { 5 op 6 } returns 2

assertEquals(2, 5 op 6)

clearStaticMockk(Int::op)

verify(exactly = 0) { 5 op 6 }
}

@Test
fun extensionPropertyStaticMock() {
mockkStatic(Int::selfOp)
Expand Down Expand Up @@ -106,4 +120,17 @@ class StaticMockkTest {

verify { 5.selfOp }
}

@Test
fun extensionPropertyClearStaticMock() {
mockkStatic(Int::selfOp)

every { 5.selfOp } returns 2

assertEquals(2, 5.selfOp)

clearStaticMockk(Int::selfOp)

verify(exactly = 0) { 5.selfOp }
}
}

0 comments on commit 4917ecc

Please sign in to comment.