Skip to content

Commit 30d5fc7

Browse files
authored
[KTLN-869] Add samples (#1013)
1 parent b73a4a0 commit 30d5fc7

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.baeldung.mockk
2+
3+
fun topLevelFunction(): String {
4+
return "Hello, World!"
5+
}
6+
7+
fun String.greet(): String {
8+
return "Hello, $this"
9+
}

kotlin-mockito/src/test/kotlin/com/baeldung/mockk/MockkStatic.kt renamed to kotlin-mockito/src/test/kotlin/com/baeldung/mockk/MockkStaticUnitTest.kt

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import org.junit.jupiter.api.Test
99
import kotlin.test.assertEquals
1010

1111

12-
class MockkStatic {
12+
class MockkStaticUnitTest {
1313

1414
@AfterEach
1515
fun `Remove RandomNumberGenerator mockks`() {
@@ -35,4 +35,20 @@ class MockkStatic {
3535
every { RandomNumberGenerator.random() } returns 0.1
3636
assertEquals(coinFlip(), "heads")
3737
}
38+
39+
@Test
40+
fun `mock top-level function`() {
41+
mockkStatic("com.baeldung.mockk.TopLevelExtensionKt")
42+
every { topLevelFunction() } returns "Mocked Response"
43+
assertEquals ("Mocked Response", topLevelFunction())
44+
unmockkStatic("com.baeldung.mockk.TopLevelExtensionKt")
45+
}
46+
47+
@Test
48+
fun `mock extension function`() {
49+
mockkStatic("com.baeldung.mockk.TopLevelExtensionKt")
50+
every { "World".greet() } returns "Mocked Greeting"
51+
assertEquals("Mocked Greeting", "World".greet())
52+
unmockkStatic("com.baeldung.mockk.TopLevelExtensionKt")
53+
}
3854
}

0 commit comments

Comments
 (0)