AssertJ solution for Mockito.verify
Asked Answered
F

2

7

Browsing through the API of AssertJ, I didn't seem to come across anything that covers the behaviour of that of Mockito.verify. Right now my assertions are all using the AssertJ fluent API, and then there's the Mockito.verify which is kind of breaking the flow of the assertions.

Is there a similar way to verify that a method is not called, called exactly once, etc. in AssertJ that I missed?

Frodine answered 6/9, 2018 at 9:8 Comment(0)
H
4

Nope, AssertJ is only an assertions library, not a mock library, there is no plan to provide mocks in the future as Mockito is already doing a great job at it.

Hosier answered 6/9, 2018 at 22:3 Comment(4)
I think the question is whether there's any implicit (possibly custom) assertion support for the mockito verify features. Maybe somewhere along the lines of verifyThat(myService).methodCalled("myMethod").withParameters(1, "2", ...).is("myExpectedResult")Hurty
Indeed. Or any other way that you can use the fluent API of AssertJ so that the 'verify' blocks don't disrupt the flow of the assertThat's. Though after an afternoon of coding, and asking opinions of colleagues, if you put the verify blocks after all assertions, it kind of looks okay. Especially playing w/ verifyZeroInteractions etc. I'm anyway going to dive into AssertJ code and see if one would be able to write an extension or your own assertThat matchers to make it possible.Frodine
Though verify is used to match interactions, not specific results of method calls. In my case, some injected services in my tested class.Frodine
Ah ok, I did not understand the question @YannickThibos can you give an example of what you would like to have? I also use verify after my assertions BTW.Hosier
D
3

I tried something like this:

SoftAssertions.assertSoftly(softly -> {
        softly.assertThat(someValue).isNull();
        softly.assertThatCode(() -> verify(mockedInstance).someCall(eq("argument")))
              .doesNotThrowAnyException();
    });
Duumvir answered 23/3, 2020 at 15:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.