Avoid Unverified Warnings When Using Mockito

I would like to make fun of the call ScheduledExecutorServiceto return the mock of the class ScheduledFuturewhen the method is called schedule. This code works beautifully:

    ScheduledExecutorService executor = Mockito.mock(ScheduledExecutorService.class);
    ScheduledFuture future = Mockito.mock(ScheduledFuture.class);
    Mockito.when(executor.schedule(
        Mockito.any(Runnable.class),
        Mockito.anyLong(),
        Mockito.any(TimeUnit.class))
    ).thenReturn(future); // <-- warning here

except that in the last line I get an unverified warning:

found raw type: java.util.concurrent.ScheduledFuture
  missing type arguments for generic class java.util.concurrent.ScheduledFuture<V>

 unchecked method invocation: method thenReturn in interface org.mockito.stubbing.OngoingStubbing is applied to given types
  required: T
  found: java.util.concurrent.ScheduledFuture

unchecked conversion
  required: T
  found:    java.util.concurrent.ScheduledFuture

Can these warnings be avoided somehow?

Type code ScheduledFuture<?> future = Mockito.mock(ScheduledFuture.class);does not compile.

+4
source share
1 answer

All warnings disappear when an alternative layout specification method is used:

    ScheduledFuture<?> future = Mockito.mock(ScheduledFuture.class);
    Mockito.doReturn(future).when(executor).schedule(
        Mockito.any(Runnable.class),
        Mockito.anyLong(),
        Mockito.any(TimeUnit.class)
    );
+2
source

Source: https://habr.com/ru/post/1621813/


All Articles