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.

Verifying multiple calls with check function logs assertion error

See original GitHub issue

Hi,

I’dont now if this is a bug. I’m trying to verify two arguments by using check {}. Of course I can use an argument capture, but I’m expecting this should working too.

import com.nhaarman.mockitokotlin2.check
import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.verify
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.mockito.internal.verification.Times

class Tweet(val id: Int, val text: String)

interface TweetRepository {
    fun persist(tweet: Tweet)
}

class TweetTest {

    @Test
    internal fun `verify multiple perists`() {
        val repositoryMock = mock<TweetRepository>()

        repositoryMock.persist(Tweet(1, "first tweet"))
        repositoryMock.persist(Tweet(2, "second tweet"))

        verify(repositoryMock, Times(1)).persist(check {
            assertThat(it.id).isEqualTo(1)
            assertThat(it.text).isEqualTo("first tweet")
        })
        verify(repositoryMock, Times(1)).persist(check {
            assertThat(it.id).isEqualTo(2)
            assertThat(it.text).isEqualTo("second tweet")
        })
    }
}

is green but logs assertion errors

org.opentest4j.AssertionFailedError: 
Expecting:
 <2>
to be equal to:
 <1>
but was not.
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

Context

  • mockito-kotlin version: 2.2.0
  • OS: OSX
  • Kotlin version: 1.3.60
  • JDK version: 11
  • JUnit version: 5.5.2
  • AssertJ version: 3.13.2
  • Type of test: unit test

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
bohsencommented, Nov 23, 2019

@larmic You should take a look at this.

0reactions
bohsencommented, Dec 9, 2019

@larmic Or create your own TweetMatcher.

But eq() is the easiest.

val tweet1 = Tweet(1, "first tweet")
val tweet2 = Tweet(2, "second tweet")
verify(repositoryMock, Times(1)).persist(eq(tweet1))
verify(repositoryMock, Times(1)).persist(eq(tweet2))
Read more comments on GitHub >

github_iconTop Results From Across the Web

Verifying multiple calls with check function logs assertion error
I'm trying to verify two arguments by using check {} . Of course I can use an argument capture, but I'm expecting this...
Read more >
java - How to verify multiple method calls with different params
OP's test case calls methodToTest() once. It is the method argument ActionErrors errors is internally called twice. – Brad. Jun 5, 2019 at...
Read more >
How to View Multiple JUnit Assertion Errors in One Test ...
Learn an easy solution to view all of the JUnit Assertion errors in one test execution from the test automation professionals at tapQA!...
Read more >
Verifying interactions | testdouble.js - GitHub Pages
First, because the API for verifying invocations of test double functions is essentially identical to the API for stubbing responses, so if you're...
Read more >
20.2. Checking Assumptions With assert - Runestone Academy
Many functions work correctly only for certain parameter values, ... The second call to sumnums(3, 1) causes the assert to fail and an...
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