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

如何告訴 Mockito 模擬對象在下次調用時返回不同

How to tell a Mockito mock object to return something different the next time it is called?(如何告訴 Mockito 模擬對象在下次調用時返回不同的東西?)
本文介紹了如何告訴 Mockito 模擬對象在下次調用時返回不同的東西?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

所以,我在類級別創建一個模擬對象作為靜態變量,就像這樣......在一個測試中,我希望 Foo.someMethod() 返回某個值,而在另一個測試中,我希望它返回一個不同的值.我遇到的問題是,我似乎需要重建模擬才能使其正常工作.我想避免重建模擬,并在每個測試中使用相同的對象.

So, I'm creating a mock object as a static variable on the class level like so... In one test, I want Foo.someMethod() to return a certain value, while in another test, I want it to return a different value. The problem I'm having is that it seems I need to rebuild the mocks to get this to work correctly. I'd like to avoid rebuilding the mocks, and just use the same objects in each test.

class TestClass {

    private static Foo mockFoo;

    @BeforeClass
    public static void setUp() {
        mockFoo = mock(Foo.class);
    }

    @Test
    public void test1() {
        when(mockFoo.someMethod()).thenReturn(0);

        TestObject testObj = new TestObject(mockFoo);

        testObj.bar(); // calls mockFoo.someMethod(), receiving 0 as the value

    }

    @Test
    public void test2() {
        when(mockFoo.someMethod()).thenReturn(1);

        TestObject testObj = new TestObject(mockFoo);

        testObj.bar(); // calls mockFoo.someMethod(), STILL receiving 0 as the value, instead of expected 1.

    }

}

在第二個測試中,當調用 testObj.bar() 時,我仍然收到 0 作為值...解決此問題的最佳方法是什么?請注意,我知道我可以在每個測試中使用不同的 Foo 模擬,但是,我必須從 mockFoo 鏈接多個請求,這意味著我必須這樣做在每個測試中鏈接.

In the second test, I'm still receiving 0 as the value when testObj.bar() is called... What is the best way to resolve this? Note that I know I could use a different mock of Foo in each test, however, I have to chain multiple requests off of mockFoo, meaning I'd have to do the chaining in each test.

推薦答案

首先不要讓 mock 靜態化.將其設為私有字段.只需將您的設置類放在 @Before 而不是 @BeforeClass 中.它可能會運行一堆,但它很便宜.

First of all don't make the mock static. Make it a private field. Just put your setUp class in the @Before not @BeforeClass. It might be run a bunch, but it's cheap.

其次,你現在擁有它的方式是讓一個模擬返回不同的東西的正確方法,具體取決于測試.

Secondly, the way you have it right now is the correct way to get a mock to return something different depending on the test.

這篇關于如何告訴 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(模擬;使用列表調用驗證方法,忽略列表中元素的順序)
主站蜘蛛池模板: 久久精品屋 | 毛片一区二区三区 | 色精品视频 | 国产精品乱码一区二三区小蝌蚪 | 午夜精品视频一区 | 欧美一级在线 | 久久久激情 | 美女视频三区 | 亚洲精品一区二区三区中文字幕 | 亚洲欧美在线视频 | 日韩在线视频一区 | 国产精品一区二区免费 | 日韩精品久久一区 | 91av精品 | 久久精品欧美视频 | www.成人久久 | 久久99久久 | 免费观看一级特黄欧美大片 | 成人国产一区二区三区精品麻豆 | 四虎影院欧美 | 午夜国产一级 | 国产农村妇女精品一二区 | 99热播精品| 99热欧美 | 自拍视频网站 | a成人| 91免费视频 | 91精品久久久久久久久久入口 | 成人在线观看免费爱爱 | aaaa网站 | 视频一区二区国产 | 精品久久久999 | 精品久久香蕉国产线看观看亚洲 | 激情网站 | 日韩国产在线 | 中文字幕一区二区三区在线观看 | 日本黄色免费大片 | 亚洲免费成人 | 日韩久久综合 | 欧美日韩国产三级 | 成人精品福利 |