問題描述
我在測試方法之外有以下方法
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
方法中執行存根來實現此功能,但我想在 @Test
s 中重復使用存根.
I can get this to work by doing the stubbing in @Test
method but I want to reuse the stubbing across @Test
s.
推薦答案
如果 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模板網!