久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 Mockito 中檢測到未完成的存根

Unfinished Stubbing Detected in Mockito(在 Mockito 中檢測到未完成的存根)
本文介紹了在 Mockito 中檢測到未完成的存根的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我在運行測試時遇到以下異常.我正在使用 Mockito 進行模擬.Mockito 庫提到的提示沒有幫助.

I am getting following exception while running the tests. I am using Mockito for mocking. The hints mentioned by Mockito library are not helping.

org.mockito.exceptions.misusing.UnfinishedStubbingException: 
Unfinished stubbing detected here:
    -> at com.a.b.DomainTestFactory.myTest(DomainTestFactory.java:355)

    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!

        at a.b.DomainTestFactory.myTest(DomainTestFactory.java:276)
        ..........

來自 DomainTestFactory 的測試代碼.當我運行以下測試時,我看到了異常.

Test Code from DomainTestFactory. When I run the following test, I see the exception.

@Test
public myTest(){
    MyMainModel mainModel =  Mockito.mock(MyMainModel.class);
    Mockito.when(mainModel.getList()).thenReturn(getSomeList()); // Line 355
}

private List<SomeModel> getSomeList() {
    SomeModel model = Mockito.mock(SomeModel.class);
    Mockito.when(model.getName()).thenReturn("SomeName"); // Line 276
    Mockito.when(model.getAddress()).thenReturn("Address");
    return Arrays.asList(model);
}

public class SomeModel extends SomeInputModel{
    protected String address;
    protected List<SomeClass> properties;

    public SomeModel() {
        this.Properties = new java.util.ArrayList<SomeClass>(); 
    }

    public String getAddress() {
        return this.address;
    }

}

public class SomeInputModel{

    public NetworkInputModel() {
        this.Properties = new java.util.ArrayList<SomeClass>(); 
    }

    protected String Name;
    protected List<SomeClass> properties;

    public String getName() {
        return this.Name;
    }

    public void setName(String value) {
        this.Name = value;
    }
}

推薦答案

你在 mocking 中嵌套了 mocking.在完成對 MyMainModel 的模擬之前,您正在調用 getSomeList(),它會進行一些模擬.當你這樣做時,Mockito 不喜歡它.

You're nesting mocking inside of mocking. You're calling getSomeList(), which does some mocking, before you've finished the mocking for MyMainModel. Mockito doesn't like it when you do this.

替換

@Test
public myTest(){
    MyMainModel mainModel =  Mockito.mock(MyMainModel.class);
    Mockito.when(mainModel.getList()).thenReturn(getSomeList()); --> Line 355
}

@Test
public myTest(){
    MyMainModel mainModel =  Mockito.mock(MyMainModel.class);
    List<SomeModel> someModelList = getSomeList();
    Mockito.when(mainModel.getList()).thenReturn(someModelList);
}

要了解這會導致問題的原因,您需要了解一點 Mockito 的工作原理,并了解 Java 中表達式和語句的計算順序.

To understand why this causes a problem, you need to know a little about how Mockito works, and also be aware in what order expressions and statements are evaluated in Java.

Mockito 無法讀取您的源代碼,因此為了弄清楚您要求它做什么,它在很大程度上依賴于靜態狀態.當您在模擬對象上調用方法時,Mockito 會將調用的詳細信息記錄在調用的內部列表中.when 方法從列表中讀取這些調用中的最后一個,并將此調用記錄在它返回的 OngoingStubbing 對象中.

Mockito can't read your source code, so in order to figure out what you are asking it to do, it relies a lot on static state. When you call a method on a mock object, Mockito records the details of the call in an internal list of invocations. The when method reads the last of these invocations off the list and records this invocation in the OngoingStubbing object it returns.

Mockito.when(mainModel.getList()).thenReturn(someModelList);

導致與 Mockito 的以下交互:

causes the following interactions with Mockito:

  • Mock 方法 mainModel.getList() 被調用,
  • 靜態方法被調用時,
  • 方法 thenReturnwhen 方法返回的 OngoingStubbing 對象上調用.
  • Mock method mainModel.getList() is called,
  • Static method when is called,
  • Method thenReturn is called on the OngoingStubbing object returned by the when method.

thenReturn 方法然后可以指示它通過 OngoingStubbing 方法接收到的模擬來處理對 getList 方法的任何合適的調用以返回 <代碼>someModelList.

The thenReturn method can then instruct the mock it received via the OngoingStubbing method to handle any suitable call to the getList method to return someModelList.

其實由于Mockito看不到你的代碼,你也可以這樣寫你的mocking:

In fact, as Mockito can't see your code, you can also write your mocking as follows:

mainModel.getList();
Mockito.when((List<SomeModel>)null).thenReturn(someModelList);

這種風格讀起來有點不太清楚,特別是因為在這種情況下 null 必須被強制轉換,但它會生成與 Mockito 相同的交互序列,并將獲得與該行相同的結果以上.

This style is somewhat less clear to read, especially since in this case the null has to be casted, but it generates the same sequence of interactions with Mockito and will achieve the same result as the line above.

但是,行

Mockito.when(mainModel.getList()).thenReturn(getSomeList());

導致與 Mockito 的以下交互:

causes the following interactions with Mockito:

  1. Mock 方法 mainModel.getList() 被調用,
  2. 靜態方法被調用時,
  3. SomeModel 的新 mock 已創建(在 getSomeList() 內),
  4. 調用模擬方法model.getName()
  1. Mock method mainModel.getList() is called,
  2. Static method when is called,
  3. A new mock of SomeModel is created (inside getSomeList()),
  4. Mock method model.getName() is called,

此時 Mockito 感到困惑.它以為你在模擬 mainModel.getList(),但現在你告訴它你想模擬 model.getName() 方法.對于 Mockito,您似乎正在執行以下操作:

At this point Mockito gets confused. It thought you were mocking mainModel.getList(), but now you're telling it you want to mock the model.getName() method. To Mockito, it looks like you're doing the following:

when(mainModel.getList());
// ...
when(model.getName()).thenReturn(...);

這對于 Mockito 來說看起來很傻,因為它無法確定您在用 mainModel.getList() 做什么.

This looks silly to Mockito as it can't be sure what you're doing with mainModel.getList().

請注意,我們沒有到達 thenReturn 方法調用,因為 JVM 需要先評估該方法的參數,然后才能調用該方法.在這種情況下,這意味著調用 getSomeList() 方法.

Note that we did not get to the thenReturn method call, as the JVM needs to evaluate the parameters to this method before it can call the method. In this case, this means calling the getSomeList() method.

通常,像 Mockito 那樣依賴靜態狀態是一個糟糕的設計決策,因為它可能導致違反最小驚訝原則的情況.然而,Mockito 的設計確實可以進行清晰而富有表現力的嘲弄,即使有時會讓人感到驚訝.

Generally it is a bad design decision to rely on static state, as Mockito does, because it can lead to cases where the Principle of Least Astonishment is violated. However, Mockito's design does make for clear and expressive mocking, even if it leads to astonishment sometimes.

最后,最新版本的 Mockito 在上面的錯誤消息中添加了額外的一行.這個額外的行表明您可能與此問題處于相同的情況:

Finally, recent versions of Mockito add an extra line to the error message above. This extra line indicates you may be in the same situation as this question:

3:如果完成,您將在thenReturn"指令之前對內部另一個模擬的行為進行存根

3: you are stubbing the behaviour of another mock inside before 'thenReturn' instruction if completed

這篇關于在 Mockito 中檢測到未完成的存根的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to mock super reference (on super class)?(如何模擬超級參考(在超級類上)?)
Java mock database connection(Java 模擬數據庫連接)
Mockito ClassCastException - A mock cannot be cast(Mockito ClassCastException - 無法投射模擬)
Set value to mocked object but get null(將值設置為模擬對象但獲取 null)
How to mock DriverManager.getConnection(...)?(如何模擬 DriverManager.getConnection(...)?)
Mockito; verify method was called with list, ignore order of elements in list(模擬;使用列表調用驗證方法,忽略列表中元素的順序)
主站蜘蛛池模板: 亚洲国产伊人 | 91精品国产乱码麻豆白嫩 | 久久久久久黄 | 免费精品视频在线观看 | 欧美video | 在线观看成人小视频 | 久久久国产精品一区 | 精品久久影院 | 国产精品一区网站 | 欧美99久久精品乱码影视 | 久久久久精| 欧美成人精品一区二区男人看 | 日韩国产一区二区三区 | 日韩伦理一区二区 | 久久久久精 | 超碰欧美 | 在线视频国产一区 | 久久久久久久久久久蜜桃 | 视频一区二区在线观看 | 天天插天天操 | 99久久免费精品国产免费高清 | 日韩欧美中文在线 | av网站在线播放 | 国产农村妇女精品一区 | 久久久精品一区 | 国产激情在线观看 | 亚洲aⅴ精品 | 日韩精品在线一区 | 中日韩av| 国产精品 亚洲一区 | 久久综合久色欧美综合狠狠 | 美女一区二区在线观看 | 亚洲天堂中文字幕 | 欧美精品一区二区免费 | 欧美一级艳情片免费观看 | 国产精品久久久久久久岛一牛影视 | av日韩精品 | 婷婷丁香在线视频 | 亚洲成a人片 | 久久精品亚洲欧美日韩精品中文字幕 | 亚洲天堂成人在线视频 |