0

I have strange behaviour. I have a unit-test that does some stuff and then performs the following verification statements:

    verify(exactly = 2) {
        observer.onThingChanged()
    }
    verify(exactly = 1) {
        b.addThing(thing)
    }
    verify(exactly = 0) {
        observer.onAnotherThingChanged(any())
    }

This test fails on the second call to verify() with the following error:

java.lang.AssertionError: Verification failed: call 1 of 1: Observer(#2).onThingChanged()).
2 matching calls found, but needs at least 1 and at most 1 calls
Calls:
1) Observer(#2).onThingChanged()
2) Observer(#2).onThingChanged()

This makes no sense. It seems to be using the "exactly" parameter from the second verify() call, but the code block from the first verify() call.

Am I missing something? Are multiple calls to verify() with different exactly= arguments supported?

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • 2
    it is indeed very weird. the scenario that you are going is totally supported - I just tried here without a problem. Can you post your entire method instead of only the verify calls? Also, if you change anything? Does it work? Like, changing everything to exactly = 2 (obvisouly it will break down the line, but will the first verify succeed?! – Henrique Vasconcellos Sep 10 '21 at 14:02
  • @HenriqueVasconcellos good to know that this is supported. By changing the values of the `exactly` parameters and setting breakpoints, I think I discovered what the problem is. The object `b` is a real object and not a mock object. It looks like you cannot verify method/function calls to real objects, only to mock objects. This was not clear to me. As soon as I changed `b` to be a mock object (and modified the rest of the test accordingly) everything worked as expected. – David Wasser Sep 10 '21 at 15:10
  • Glad that it worked! Exactly if you try to verify something that is not mocked, Mockk cannot "see" the object being called. – Henrique Vasconcellos Sep 10 '21 at 17:13
  • @HenriqueVasconcellos I understand that,but the error message that's given is completely wrong! It sent me on a wild goose chase! I would prefer an error saying "you can't verify actions on non-mocked objects!" – David Wasser Sep 10 '21 at 17:32

0 Answers0