question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

In 1.12.3 `verify { mock.method( withArg {}) }` fails if second call matches. Works in 1.12.2

See original GitHub issue

Prerequisites

  • I am running the latest version
  • I checked the documentation and found no answer
  • I checked to make sure that this issue has not already been filed

Expected Behavior

When calling verification using withArg{}, if there are multiple calls to mock method, and one of calls matches withArg, the verification should pass.

Current Behavior

If there are multiple calls to method verification will fail. It passed in 1.12.2

Context

Please provide any relevant information about your setup. This is important in case the issue is not reproducible except for under certain conditions.

  • MockK version: 1.12.3
  • OS: Win/Linux
  • Kotlin version: 1.6.10
  • JDK version: 17-azul
  • JUnit version: 5.8.2
  • Unit Test

Minimal reproducible code (the gist of this issue)

@Test
fun `mockk can assert one of calls`() {
    open class Mocked {
        fun called(i: Int) {}
    }
    val mock = mockk<Mocked>()
    every { mock.called(any()) } just Runs
    mock.called(1)
    mock.called(3)
    verify {
        mock.called(withArg { assert(it == 3) })
    }
}

I would guess this is caused by https://github.com/mockk/mockk/pull/776, as https://github.com/mockk/mockk/pull/394 fixed https://github.com/mockk/mockk/issues/389 that seemed similar, and https://github.com/mockk/mockk/pull/776 reverted changes.

If this is intentional change, please provide intended way of asserting with many calls.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:7

github_iconTop GitHub Comments

2reactions
Raibazcommented, Mar 14, 2022

As a side note, most of the code that I have seen doing

verify {
    mock.method(withArg {
        assertEquals("someValue", it.property)
    })
}

Can also be written as

verify {
    mock.method(match {
        it.property == "someValue"
    })
}

Which is, IMHO, pretty concise for a few properties to be checked.

In case someone needs to verify more than a few properties, I’d probably still go for a slot.

0reactions
piotrplazienskicommented, Mar 14, 2022

Yes, I think using predicate is better practice and that is where I will go with rework of my tests.

I will close the issue now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mockito. Verify method arguments - java - Stack Overflow
So mockito expects your verifier silently returns true for one of the argument set, and false (no assert exceptions) for other valid calls....
Read more >
Assertions with an argument | MockK Guidebook
Run assertions with an argument #. There are some special argument matchers that can only be used when verifying that a mocked function...
Read more >
mockk-io/Lobby - Gitter
I'm stubbing, or trying to stub, a call to a mock which one of its parameters is a ... import io.mockk.mockk import io.mockk.verify...
Read more >
Mockito ArgumentMatchers - Baeldung
We can't use argument matchers outside of verification or stubbing. As per the second point, Mockito will detect the misplaced argument and ...
Read more >
How to verify in order method invocation while capturing ...
B, and when I call a method of class A I want to verify that certain ... liberty of rewriting the tests for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found