unit-testingspring-webfluxproject-reactorretrywhen

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


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


Solution

  • 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