加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – Mockito在测试方法之外的存根

发布时间:2020-12-14 23:47:25 所属栏目:Java 来源:网络整理
导读:我在测试方法之外有以下方法 private DynamicBuild getSkippedBuild() { DynamicBuild build = mock(DynamicBuild.class); when(build.isSkipped()).thenReturn(true); return build;} 但是当我调用这个方法时,我得到以下错误 org.mockito.exceptions.misusi
我在测试方法之外有以下方法
private DynamicBuild getSkippedBuild() {
    DynamicBuild build = mock(DynamicBuild.class);
    when(build.isSkipped()).thenReturn(true);
    return build;
}

但是当我调用这个方法时,我得到以下错误

org.mockito.exceptions.misusing.UnfinishedStubbingException: 
Unfinished stubbing detected here:
-> at LINE BEING CALLED FROM

E.g. thenReturn() may be missing.
Examples of correct stubbing:
    when(mock.isOk()).thenReturn(true);
    when(mock.isOk()).thenThrow(exception);
    doThrow(exception).when(mock).someVoidMethod();
Hints:
 1. missing thenReturn()
 2. you are trying to stub a final method,you naughty developer!

当你在测试方法之外存在时,看起来mockito不高兴.这不受支持吗?

编辑:我可以通过在@Test方法中进行存根来实现这一点,但我想重用@Tests中的存根.

解决方法

如果isSkipped()不是最终方法,则此问题可能表示您尝试在另一个方法的存根正在进行时存根方法.它不受支持,因为Mockito依赖于其存根API中的方法调用顺序(when()等).

我想你的测试方法中有这样的东西:

when(...).thenReturn(getSkippedBuild());

如果是这样,您需要重写如下:

DynamicBuild build = getSkippedBuild();
when(...).thenReturn(build);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读