1
+ package com.baeldung.mockk
2
+
3
+ import io.mockk.*
4
+ import org.junit.jupiter.api.Test
5
+ import org.junit.jupiter.api.assertThrows
6
+ import kotlin.test.assertEquals
7
+
8
+ class GreetingService {
9
+ fun greeting (name : String ) = " $name , how are you?"
10
+
11
+ companion object {
12
+ fun sayGoodDay (name : String ) = " $name , good day!"
13
+ }
14
+ }
15
+
16
+ object GoodMorning {
17
+ fun sayGoodMorning (name : String ) = " $name , good morning!"
18
+ }
19
+
20
+ fun sayGoodNight (name : String ) = " $name , good night!"
21
+
22
+ class ClearAllMocksVsUnmockkAllUnitTest {
23
+
24
+ @Test
25
+ fun `when using clearAllMocks() then Mock objects are cleared` () {
26
+ val greetingServiceMock = mockk<GreetingService > {
27
+ every { greeting(any()) } returns " mocked instance"
28
+ }
29
+ assertEquals(" mocked instance" , greetingServiceMock.greeting(" Kai" ))
30
+
31
+ clearAllMocks()
32
+
33
+ assertThrows<MockKException > {
34
+ greetingServiceMock.greeting(" Kai" )
35
+ }.also { it.message?.startsWith(" no answer found for: com.baeldung.mockk.GreetingService" ) }
36
+ }
37
+
38
+ @Test
39
+ fun `when using unmockkAll() then Mock objects are still available` () {
40
+ val greetingServiceMock = mockk<GreetingService > {
41
+ every { greeting(any()) } returns " mocked instance"
42
+ }
43
+ assertEquals(" mocked instance" , greetingServiceMock.greeting(" Kai" ))
44
+
45
+ unmockkAll()
46
+
47
+ assertEquals(" mocked instance" , greetingServiceMock.greeting(" Kai" ))
48
+ }
49
+
50
+
51
+ @Test
52
+ fun `when using clearAllMocks() then mocks are cleared` () {
53
+ mockkObject(GreetingService )
54
+ every { GreetingService .sayGoodDay(any()) } returns " mocked static fun"
55
+ assertEquals(" mocked static fun" , GreetingService .sayGoodDay(" Kai" ))
56
+
57
+ mockkObject(GoodMorning )
58
+ every { GoodMorning .sayGoodMorning(any()) } returns " mocked object"
59
+ assertEquals(" mocked object" , GoodMorning .sayGoodMorning(" Kai" ))
60
+
61
+ mockkStatic(::sayGoodNight)
62
+ every { sayGoodNight(any()) } returns " mocked top-level fun"
63
+ assertEquals(" mocked top-level fun" , sayGoodNight(" Kai" ))
64
+
65
+ clearAllMocks()
66
+
67
+ assertEquals(" Kai, good day!" , GreetingService .sayGoodDay(" Kai" ))
68
+ assertEquals(" Kai, good morning!" , GoodMorning .sayGoodMorning(" Kai" ))
69
+ assertEquals(" Kai, good night!" , sayGoodNight(" Kai" ))
70
+ }
71
+
72
+ @Test
73
+ fun `when using unmockkAll() then mocks are unmocked` () {
74
+ mockkObject(GreetingService )
75
+ every { GreetingService .sayGoodDay(any()) } returns " mocked static fun"
76
+ assertEquals(" mocked static fun" , GreetingService .sayGoodDay(" Kai" ))
77
+
78
+ mockkObject(GoodMorning )
79
+ every { GoodMorning .sayGoodMorning(any()) } returns " mocked object"
80
+ assertEquals(" mocked object" , GoodMorning .sayGoodMorning(" Kai" ))
81
+
82
+ mockkStatic(::sayGoodNight)
83
+ every { sayGoodNight(any()) } returns " mocked top-level fun"
84
+ assertEquals(" mocked top-level fun" , sayGoodNight(" Kai" ))
85
+
86
+ unmockkAll()
87
+
88
+ assertEquals(" Kai, good day!" , GreetingService .sayGoodDay(" Kai" ))
89
+ assertEquals(" Kai, good morning!" , GoodMorning .sayGoodMorning(" Kai" ))
90
+ assertEquals(" Kai, good night!" , sayGoodNight(" Kai" ))
91
+ }
92
+
93
+ @Test
94
+ fun `after calling clearAllMocks() then the mock can be reused` () {
95
+ mockkStatic(::sayGoodNight)
96
+ every { sayGoodNight(any()) } returns " mocked top-level fun"
97
+ assertEquals(" mocked top-level fun" , sayGoodNight(" Kai" ))
98
+
99
+ clearAllMocks()
100
+
101
+ assertEquals(" Kai, good night!" , sayGoodNight(" Kai" ))
102
+
103
+ every { sayGoodNight(any()) } returns " get mocked again"
104
+ assertEquals(" get mocked again" , sayGoodNight(" Kai" ))
105
+ }
106
+
107
+ @Test
108
+ fun `after calling unmockkAll() then the mock cannot be reused` () {
109
+ mockkStatic(::sayGoodNight)
110
+ every { sayGoodNight(any()) } returns " mocked top-level fun"
111
+ assertEquals(" mocked top-level fun" , sayGoodNight(" Kai" ))
112
+
113
+ unmockkAll()
114
+
115
+ assertEquals(" Kai, good night!" , sayGoodNight(" Kai" ))
116
+ assertThrows<MockKException > {
117
+ every { sayGoodNight(any()) } returns " mocked again"
118
+ }.also { it.message?.startsWith(" Failed matching mocking signature" ) }
119
+
120
+ mockkStatic(::sayGoodNight)
121
+ every { sayGoodNight(any()) } returns " mocked again"
122
+ assertEquals(" mocked again" , sayGoodNight(" Kai" ))
123
+
124
+ }
125
+
126
+ }
0 commit comments