Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AssertJ solution for Mockito.verify

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?

like image 208
Yannick Thibos Avatar asked Sep 06 '18 09:09

Yannick Thibos


People also ask

How do you verify a method called in Mockito?

Mockito verify() simple example It's the same as calling with times(1) argument with verify method. verify(mockList, times(1)). size(); If we want to make sure a method is called but we don't care about the argument, then we can use ArgumentMatchers with verify method.

What does verify method do in Mockito?

Mockito can ensure whether a mock method is being called with reequired arguments or not. It is done using the verify() method.

Does Mockito verify use equals?

Mockito verifies argument values in natural java style: by using an equals() method.


2 Answers

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.

like image 58
Joel Costigliola Avatar answered Oct 20 '22 09:10

Joel Costigliola


I tried something like this:

SoftAssertions.assertSoftly(softly -> {
        softly.assertThat(someValue).isNull();
        softly.assertThatCode(() -> verify(mockedInstance).someCall(eq("argument")))
              .doesNotThrowAnyException();
    });
like image 3
Cristian-Petrut Petrache Avatar answered Oct 20 '22 09:10

Cristian-Petrut Petrache