File tree Expand file tree Collapse file tree 2 files changed +26
-1
lines changed
main/kotlin/com/baeldung/mockk
test/kotlin/com/baeldung/mockk Expand file tree Collapse file tree 2 files changed +26
-1
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -9,7 +9,7 @@ import org.junit.jupiter.api.Test
9
9
import kotlin.test.assertEquals
10
10
11
11
12
- class MockkStatic {
12
+ class MockkStaticUnitTest {
13
13
14
14
@AfterEach
15
15
fun `Remove RandomNumberGenerator mockks` () {
@@ -35,4 +35,20 @@ class MockkStatic {
35
35
every { RandomNumberGenerator .random() } returns 0.1
36
36
assertEquals(coinFlip(), " heads" )
37
37
}
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
+ }
38
54
}
You can’t perform that action at this time.
0 commit comments