Skip to content

Commit 9292631

Browse files
authored
Merge pull request #1239 from krocard/clearStaticMockk
Implement `clearStaticMockk` for KFunction and KProperty
2 parents 143f6bb + 8e024f0 commit 9292631

3 files changed

Lines changed: 41 additions & 0 deletions

File tree

modules/mockk/api/mockk.api

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
public final class io/mockk/JVMMockKKt {
2+
public static final fun clearStaticMockk ([Lkotlin/reflect/KFunction;)V
3+
public static final fun clearStaticMockk ([Lkotlin/reflect/KProperty;)V
24
public static final fun getDeclaringKotlinFile (Lkotlin/reflect/KFunction;)Lkotlin/reflect/KClass;
35
public static final fun mockkStatic ([Lkotlin/reflect/KFunction;)V
46
public static final fun mockkStatic ([Lkotlin/reflect/KFunction;Lkotlin/jvm/functions/Function0;)V

modules/mockk/src/jvmMain/kotlin/io/mockk/MockK.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,18 @@ fun unmockkStatic(vararg functions: KFunction<*>) =
4444
fun unmockkStatic(vararg functions: KProperty<*>) =
4545
unmockkStatic(*functions.map(KProperty<*>::getter).toTypedArray())
4646

47+
/**
48+
* Clear static mocks.
49+
*/
50+
fun clearStaticMockk(vararg functions: KFunction<*>) =
51+
clearStaticMockk(*functions.map { it.declaringKotlinFile }.toTypedArray())
52+
53+
/**
54+
* Clear static mocks.
55+
*/
56+
fun clearStaticMockk(vararg functions: KProperty<*>) =
57+
clearStaticMockk(*functions.map(KProperty<*>::getter).toTypedArray())
58+
4759
/**
4860
* Builds a static mock and unmocks it after the block has been executed.
4961
*/

modules/mockk/src/jvmTest/kotlin/io/mockk/it/StaticMockkTest.kt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.mockk.it
22

3+
import io.mockk.clearStaticMockk
34
import io.mockk.every
45
import io.mockk.mockkStatic
56
import io.mockk.unmockkStatic
@@ -73,6 +74,19 @@ class StaticMockkTest {
7374
verify { 5 op 6 }
7475
}
7576

77+
@Test
78+
fun extensionFunctionClearStaticMock() {
79+
mockkStatic(Int::op)
80+
81+
every { 5 op 6 } returns 2
82+
83+
assertEquals(2, 5 op 6)
84+
85+
clearStaticMockk(Int::op)
86+
87+
verify(exactly = 0) { 5 op 6 }
88+
}
89+
7690
@Test
7791
fun extensionPropertyStaticMock() {
7892
mockkStatic(Int::selfOp)
@@ -106,4 +120,17 @@ class StaticMockkTest {
106120

107121
verify { 5.selfOp }
108122
}
123+
124+
@Test
125+
fun extensionPropertyClearStaticMock() {
126+
mockkStatic(Int::selfOp)
127+
128+
every { 5.selfOp } returns 2
129+
130+
assertEquals(2, 5.selfOp)
131+
132+
clearStaticMockk(Int::selfOp)
133+
134+
verify(exactly = 0) { 5.selfOp }
135+
}
109136
}

0 commit comments

Comments
 (0)