Skip to content

Commit

Permalink
Merge pull request #1239 from krocard/clearStaticMockk
Browse files Browse the repository at this point in the history
Implement `clearStaticMockk` for KFunction and KProperty
  • Loading branch information
Raibaz committed Apr 3, 2024
2 parents 143f6bb + 8e024f0 commit 9292631
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
2 changes: 2 additions & 0 deletions modules/mockk/api/mockk.api
@@ -1,4 +1,6 @@
public final class io/mockk/JVMMockKKt {
public static final fun clearStaticMockk ([Lkotlin/reflect/KFunction;)V
public static final fun clearStaticMockk ([Lkotlin/reflect/KProperty;)V
public static final fun getDeclaringKotlinFile (Lkotlin/reflect/KFunction;)Lkotlin/reflect/KClass;
public static final fun mockkStatic ([Lkotlin/reflect/KFunction;)V
public static final fun mockkStatic ([Lkotlin/reflect/KFunction;Lkotlin/jvm/functions/Function0;)V
Expand Down
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 9292631

Please sign in to comment.