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

使用 Mockito 驗(yàn)證方法后沒有調(diào)用任何內(nèi)容

Use Mockito to verify that nothing is called after a method(使用 Mockito 驗(yàn)證方法后沒有調(diào)用任何內(nèi)容)
本文介紹了使用 Mockito 驗(yàn)證方法后沒有調(diào)用任何內(nèi)容的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問題描述

我正在使用 Mockito 編寫 Java 單元測(cè)試,我想驗(yàn)證某個(gè)方法是否是對(duì)象上調(diào)用的最后一個(gè).

I'm using Mockito to write a unit test in Java, and I'd like to verify that a certain method is the last one called on an object.

我在被測(cè)代碼中做了這樣的事情:

I'm doing something like this in the code under test:

row.setSomething(value);
row.setSomethingElse(anotherValue);
row.editABunchMoreStuff();
row.saveToDatabase();

在我的模擬中,我不關(guān)心編輯行中所有內(nèi)容的順序,但重要的是我在保存后嘗試對(duì)其執(zhí)行更多操作它.有沒有好的方法來(lái)做到這一點(diǎn)?

In my mock, I don't care about the order in which I edit everything on the row, but it's very important that I not try to do anything more to it after I've saved it. Is there a good way to do this?

請(qǐng)注意,我不是在尋找 verifyNoMoreInteractions:它不會(huì)確認(rèn) saveToDatabase 是最后調(diào)用的東西,如果我調(diào)用行上沒有明確驗(yàn)證的任何內(nèi)容,它也會(huì)失敗.我希望能夠這樣說(shuō):

Note that I'm not looking for verifyNoMoreInteractions: it doesn't confirm that saveToDatabase is the last thing called, and it also fails if I call anything on the row that I don't explicitly verify. I'd like to be able to say something like:

verify(row).setSomething(value);
verify(row).setSomethingElse(anotherValue);
verifyTheLastThingCalledOn(row).saveToDatabase();

如果有幫助,我將從執(zhí)行此操作的 JMock 測(cè)試切換到 Mockito:

If it helps, I'm switching to Mockito from a JMock test that did this:

row.expects(once()).method("saveToDatabase").id("save");
row.expects(never()).method(ANYTHING).after("save");

推薦答案

我認(rèn)為這需要更多的自定義工作.

I think it requires more custom work.

verify(row, new LastCall()).saveToDatabase();

然后

public class LastCall implements VerificationMode {
    public void verify(VerificationData data) {
        List<Invocation> invocations = data.getAllInvocations();
        InvocationMatcher matcher = data.getWanted();
        Invocation invocation = invocations.get(invocations.size() - 1);
        if (!matcher.matches(invocation)) throw new MockitoException("...");
    }
}

上一個(gè)答案:

你是對(duì)的.verifyNoMoreInteractions 是您所需要的.

You are right. verifyNoMoreInteractions is what you need.

verify(row).setSomething(value);
verify(row).setSomethingElse(anotherValue);
verify(row).editABunchMoreStuff();
verify(row).saveToDatabase();
verifyNoMoreInteractions(row);

這篇關(guān)于使用 Mockito 驗(yàn)證方法后沒有調(diào)用任何內(nèi)容的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

How to mock super reference (on super class)?(如何模擬超級(jí)參考(在超級(jí)類上)?)
Java mock database connection(Java 模擬數(shù)據(jù)庫(kù)連接)
Mockito ClassCastException - A mock cannot be cast(Mockito ClassCastException - 無(wú)法投射模擬)
Set value to mocked object but get null(將值設(shè)置為模擬對(duì)象但獲取 null)
How to mock DriverManager.getConnection(...)?(如何模擬 DriverManager.getConnection(...)?)
Mockito; verify method was called with list, ignore order of elements in list(模擬;使用列表調(diào)用驗(yàn)證方法,忽略列表中元素的順序)
主站蜘蛛池模板: 国产一区高清 | 一区二区三区视频在线 | 国产精品久久视频 | 成人一区二区三区在线观看 | www.欧美.com| 国产日韩精品一区二区三区 | 国产精品亚洲视频 | 国产欧美在线播放 | 亚洲一区二区三区四区五区午夜 | 亚洲第一福利网 | 国产一区二区三区在线免费 | 色精品 | 一区二区三区视频在线 | 久久久久久久av麻豆果冻 | 国产亚洲成av人片在线观看桃 | 色综合av | 亚洲精品在线91 | 亚洲精品乱码8久久久久久日本 | 一呦二呦三呦国产精品 | 视频一区中文字幕 | 国色天香成人网 | 日韩av在线免费 | 国产在线一区二区 | 亚洲精品久久久久久久久久吃药 | 日本精品视频在线观看 | 欧美精品久久久久 | 婷婷桃色网 | 中文字幕亚洲区一区二 | 在线播放第一页 | 免费日本视频 | 国产欧美视频一区二区三区 | 久久久久久国模大尺度人体 | 国产99久久 | 精品亚洲视频在线 | 成人av免费播放 | 在线观看国产www | 成人欧美一区二区三区视频xxx | 精品国产一区二区三区免费 | 国产欧美日韩综合精品一区二区 | 国产日韩精品视频 | 亚洲一二三区在线观看 |