Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #518 from gmazzo/feature/mockstatic-hard-reference…
…-support Added `mockkStatic` hard reference support
- Loading branch information
Showing
3 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,38 @@ | ||
@file:JvmName("JVMMockKKt") | ||
package io.mockk | ||
|
||
import io.mockk.impl.JvmMockKGateway | ||
import kotlin.reflect.KFunction | ||
import kotlin.reflect.jvm.javaMethod | ||
|
||
actual object MockK { | ||
actual inline fun <T> useImpl(block: () -> T): T { | ||
MockKGateway.implementation = JvmMockKGateway.defaultImplementationBuilder | ||
return block() | ||
} | ||
} | ||
|
||
/** | ||
* Returns the defining top-level extension function's [Class]. | ||
*/ | ||
inline val KFunction<*>.declaringKotlinFile | ||
get() = checkNotNull(javaMethod) { "$this is not a top-level extension function" } | ||
.declaringClass.kotlin | ||
|
||
/** | ||
* Builds a static mock. Any mocks of this function's declaring class are cancelled before it's mocked | ||
*/ | ||
inline fun mockkStatic(vararg functions: KFunction<*>) = | ||
mockkStatic(*functions.map { it.declaringKotlinFile }.toTypedArray()) | ||
|
||
/** | ||
* Cancel static mocks. | ||
*/ | ||
inline fun unmockkStatic(vararg functions: KFunction<*>) = | ||
unmockkStatic(*functions.map { it.declaringKotlinFile }.toTypedArray()) | ||
|
||
/** | ||
* Builds a static mock and unmocks it after the block has been executed. | ||
*/ | ||
inline fun mockkStatic(vararg functions: KFunction<*>, block: () -> Unit) = | ||
mockkStatic(classes = *functions.map { it.declaringKotlinFile }.toTypedArray(), block = block) |
45 changes: 45 additions & 0 deletions
45
mockk/jvm/src/test/kotlin/io/mockk/it/StaticMockJVMTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package io.mockk.it | ||
|
||
import io.mockk.every | ||
import io.mockk.mockkStatic | ||
import io.mockk.verify | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class StaticMockJVMTest { | ||
|
||
@Test | ||
fun simpleStaticMock() { | ||
mockkStatic(Int::op) | ||
|
||
every { 5 op 6 } returns 2 | ||
|
||
assertEquals(2, 5 op 6) | ||
|
||
verify { 5 op 6 } | ||
} | ||
|
||
@Test | ||
fun clearStateStaticMock() { | ||
mockkStatic(Int::op) | ||
|
||
every { 5 op 6 } returns 2 | ||
|
||
assertEquals(2, 5 op 6) | ||
|
||
verify { 5 op 6 } | ||
|
||
mockkStatic(Int::op) | ||
|
||
verify(exactly = 0) { 5 op 6 } | ||
|
||
assertEquals(11, 5 op 6) | ||
|
||
every { 5 op 6 } returns 3 | ||
|
||
assertEquals(3, 5 op 6) | ||
|
||
verify { 5 op 6 } | ||
} | ||
|
||
} |