我在测试方法之外有以下方法
I have the following method outside the test method
private DynamicBuild getSkippedBuild() {
DynamicBuild build = mock(DynamicBuild.class);
when(build.isSkipped()).thenReturn(true);
return build;
}
但是当我调用这个方法时,我得到了以下错误
but when I call this method I get the following error
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 似乎不高兴.不支持吗?
Looks like mockito is not happy when you stub outside the test method. Is that not supported ?
我可以通过在 @Test 方法中执行存根来实现此功能,但我想在 @Tests 中重复使用存根.
I can get this to work by doing the stubbing in @Test method but I want to reuse the stubbing across @Tests.
如果 isSkipped() 不是 final 方法,这个问题可能表明你尝试存根一种方法,而另一种方法的存根正在进行中.它不受支持,因为 Mockito 在其存根 API 中依赖于方法调用的顺序(when() 等).
If isSkipped() is not a final method, this problem probably indicates that you try to stub a method while stubbing of another method is in progress. It's not supported because Mockito relies on order of method invocations (when(), etc) in its stubbing API.
我猜你的测试方法中有这样的东西:
I guess you have something like this in your test method:
when(...).thenReturn(getSkippedBuild());
如果是,则需要改写如下:
If so, you need to rewrite it as follows:
DynamicBuild build = getSkippedBuild();
when(...).thenReturn(build);
这篇关于测试方法之外的 Mockito 存根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
如何检测 32 位 int 上的整数溢出?How can I detect integer overflow on 32 bits int?(如何检测 32 位 int 上的整数溢出?)
return 语句之前的局部变量,这有关系吗?Local variables before return statements, does it matter?(return 语句之前的局部变量,这有关系吗?)
如何将整数转换为整数?How to convert Integer to int?(如何将整数转换为整数?)
如何在给定范围内创建一个随机打乱数字的 intHow do I create an int array with randomly shuffled numbers in a given range(如何在给定范围内创建一个随机打乱数字的 int 数组)
java的行为不一致==Inconsistent behavior on java#39;s ==(java的行为不一致==)
为什么 Java 能够将 0xff000000 存储为 int?Why is Java able to store 0xff000000 as an int?(为什么 Java 能够将 0xff000000 存储为 int?)