3

mockk 1.9.3

In the test noticed if did static mock in previous test, the next test will be using same mock. Thought to do a reset at @After, but not sure which one to use clearAllMocks or unmockkAll.

in https://mockk.io/

unmockkAll  unmocks object, static and constructor mocks
clearAllMocks   clears regular, object, static and constructor mocks

but not clear what are the difference by unmocks and clears.

e.g.

@Test
fun test_1() {
    mockkStatic(TextUtils::class)
    every { TextUtils.isEmpty(param } returns true

    //test
    doSomeThingUsingTextUtils()
    // verify
    ... ...
}
@Test
fun test_2() {
    // in this test it does not want the mocked stub behavior

}

What it should use, clear or 'unmock`?

lannyf
  • 9,865
  • 12
  • 70
  • 152

2 Answers2

3

For me, understanding the difference between Clearing and Unmocking was sufficient.

clear - deletes internal state of objects associated with mock resulting in empty object

unmock - re-assigns transformation of classes back to original state prior to mock (Source)

PS: I understand the confusion! I had it as well!

Let me know if you have any questions. Thanks.

minchaej
  • 1,294
  • 1
  • 7
  • 14
0

clearAllMocks() will undo every { TextUtils.isEmpty(param } returns true.

while unmockkAll() will undo mockkStatic(TextUtils::class)

Danzel
  • 1