Verification matcher for inline asserting

48 views
Skip to first unread message

Marcin Zajączkowski

unread,
Jan 26, 2022, 5:26:31 AM1/26/22
to mockito
Hi. I've been treating my mockito-java8 as a deprecated project - with all the major features already available in mockito-core. However, recently, I've been migrating some big project from Mockito 1.x to 2.x/4.x and I wasn't able to find a simple way to perform inline asserting in verification, such as:

```
    @Test
    void shouldAllowToUseAssertionInLambdaWithPrimitiveAsArgument() {
        //when
        ts.fireTorpedo(2);
        //then
        verify(ts).fireTorpedo(assertArg(i -> assertThat(i).isEqualTo(2)));
    }
```

As ArgumentMacher has a return type, it is problematic to do it in one line (an extra "return true" is required - a custom Matcher has to be created).

Of course it can also be done with ArgumentCaptor, but it is even 3 lines instead od 1.

The implementation in mockito-java8 is trivial:
https://github.com/szpak/mockito-java8/blob/7c9f30b0d53445a9dd0c7a380cdab58a0079d716/src/main/java/info/solidsoft/mockito/java8/AssertionMatcher.java#L50

If I missed something existing, please let me know. In the other case, I wonder, if you are open to accept assertArg() addition to the mockito-core (to be able to officially deprecated mockito-java8 :) )?


Marcin

--
https://blog.solidsoft.pl/ - Solid Soft - Working code is not enough

Tim van der Lippe

unread,
Jan 26, 2022, 5:27:29 PM1/26/22
to mockito
Hey Marcin,

Yes, that makes sense to me. I think the best place to add this method would be `AdditionalMatchers`. Looking forward to the PR!

Cheers,
Tim

Op wo 26 jan. 2022 om 10:26 schreef Marcin Zajączkowski <msz...@wp.pl>:
--
You received this message because you are subscribed to the Google Groups "mockito" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mockito+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mockito/4dfc185d-ef4f-4d2c-8bf4-3525145b878cn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages