I have the following code which I want to test:
@Slf4j
@Component
public class DispatcherTask {
private final MyClassService myClassService;
private final SreamingService streamingService;
private ExecutorService executor = Executors.newCachedThreadPool();
private final Set < String > dispatching = new ConcurrentSkipListSet < > ();
private final RetryPolicy < Object > httpRetryPolicy;
public DispatcherTask(MyClassService myClassService, SreamingService streamingService) {
this.myClassService = myClassService;
this.streamingService = streamingService;
this.httpRetryPolicy = new RetryPolicy < > ()
.handle(HttpClientErrorException.class)
.onRetriesExceeded(e - > log.warn("Max retries have been exceeded for http exception"))
.withBackoff(2, 3, ChronoUnit.SECONDS)
.withMaxRetries(5);
}
public void initMethod(MyClass myclass) {
Failsafe.with(httpRetryPolicy).with(executor)
.onFailure((e) - > {
// TODO log.error();
myClassService.updateStatus(myclass.getId(), Status.FAILED);
})
.onSuccess((o) - > {
MyClass updatedMyClass = myClassService.updateStatus(myclass.getId(), GameStatus.ENDED);
streamingService.storeData(updateMyClass);
dispatching.remove(myclass.getPlatformId());
//TODO log.info()
})
.runAsync(() - > {
MyClass updatedMyClass =
myClassService.updateStatus(myclass.getId(), Status.STREAMING);
streamingService.polling(StreamedClass.builder().myclass(updatedMyClass).build());
// TODO log.info()
});
}
}
This is my JUnit test with Mockito
:
@ExtendWith(MockitoExtension.class)
public class DispatcherTaskTest {
@Mock private MyClassService myClassService;
@Mock private StreamingService streamingService;
@InjectMocks private DispatcherTask dispatcherTask;
@Test
void test() throws InterruptedException {
//given
MyClass myclass = new MyClass().setId(1L).setName("name").setStatus(Status.CREATED);
when(myClassService.updateStatus(myclass.getId(), Status.STREAMING)).thenReturn(myclass);
when(myClassService.updateStatus(myclass.getId(), Status.ENDED)).thenReturn(myclass);
doNothing().when(streamingService).polling(any());
doNothing().when(streamingService).storeData(any());
//when
dispatcherTask.initMethod(myclass)
//then
verify(myClassService, times(1)).updateStatus(myclass.getId(), Status.STREAMING);
verify(myClassService, times(1)).updateStatus(myclass.getId(), Status.ENDED);
}
}
If I run it like that the last verify that checks the status ENDED
fails. If I add a Thread.sleep(3l);
it passes. Is there a better or more secure way to pass the test without adding the sleep()
method?