当前位置:  开发笔记 > 编程语言 > 正文

使用Mockito时避免未经检查的警告

如何解决《使用Mockito时避免未经检查的警告》经验,为你挑选了1个好方法。

我想在调用方法时模拟调用on ScheduledExecutorService来返回ScheduledFuture类的mock schedule.以下代码编译并正常工作:

    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

除了我在最后一行得到未经检查的警告:

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

 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

是否有可能以某种方式避免这些警告?

代码就像ScheduledFuture future = Mockito.mock(ScheduledFuture.class);不编译.



1> Michal Korda..:

当使用替代的模拟规范方法时,所有警告都会消失:

    ScheduledFuture future = Mockito.mock(ScheduledFuture.class);
    Mockito.doReturn(future).when(executor).schedule(
        Mockito.any(Runnable.class),
        Mockito.anyLong(),
        Mockito.any(TimeUnit.class)
    );

推荐阅读
黄晓敏3023
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有