Home > other >  How to verify number of invocations, in the project reactor for retryWhen
How to verify number of invocations, in the project reactor for retryWhen

Time:11-04

I have the following function public Mono<Integer> revertChange() { someService.someMethod() .retryWhen(3 times, with 150millis of delay, if specific error occured) .onError(e -> log_the_error); } And I have a simple unit test that summpose to verify that the someService.someMethod was called exactly 3 times `class Test {

@InjectMocks
SomeService someService;

@Test
void shouldCallSomeServiceExactlythreetimes_whenErrorOccured() {
    verify(someService).someMethod(3)//someMethod invoked 3 times
}

} `

The problem is that the verify block does not catches that the someMethod was executed 3 times, it says only 1. I am using junit 5 and jmockit, maybe there are better alternatives specific for reactive mocks, any ideas guys?

Verification block does not catch multiple execution of the method

CodePudding user response:

Mockito.verify(someService, Mockito.times(3)).someMethod();

from https://javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/Mockito.html :

Arguments passed are compared using equals() method. Read about ArgumentCaptor or ArgumentMatcher to find out other ways of matching / asserting arguments passed.

Although it is possible to verify a stubbed invocation, usually it's just redundant. Let's say you've stubbed foo.bar(). If your code cares what foo.bar() returns then something else breaks(often before even verify() gets executed). If your code doesn't care what foo.bar() returns then it should not be stubbed.

check also https://javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/Mockito.html#4

for verification with time out check https://javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/Mockito.html#verification_timeout

this snippet passes as soon as someMethod() has been called 2 times under 150 ms Mockito.verify(someService, Mockito.timeout(150).times(2)).someMethod();

CodePudding user response:

After careful investigation of the problem and deep dive to project reactor internals, the solution looks pretty simple the method that I am calling needs to be wrapped with Mono.deffer(() -> someService.someMethod()) that will eagerly every time when you call it

  • Related