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

在測試中的方法內部創建的模擬對象

Mocking Objects Created Inside method Under test(在測試中的方法內部創建的模擬對象)
本文介紹了在測試中的方法內部創建的模擬對象的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個我想測試的類.只要有可能,我就會對依賴于其他類對象的類進行依賴注入.但是,我遇到了一個案例,我想在不重組對象的情況下模擬對象代碼而不是應用 DI.

I have a class which I would like to test.Whenever possible I would do dependency injections for that class which depends on object of other classes.But,I ran into a case where I would like to mock the object without restructuring the code and not appling DI.

這是被測試的類:

public class Dealer {

    public int show(CarListClass car){
        Print print=new Print();

        List<String> list=new LinkedList<String>();

        list=car.getList();
        System.out.println("Size of car list :"+list.size());

        int printedLines=car.printDelegate(print);
        System.out.println("Num of lines printed"+printedLines);

        return num;
    }
}

我的測試類是:

public class Tester {
    Dealer dealer;

     CarListClass car=mock(CarListClass.class);  
     List<String> carTest;
     Print print=mock(Print.class);

    @Before
    public void setUp() throws Exception {
        dealer=new Dealer();
        carTest=new LinkedList<String>();
        carTest.add("FORD-Mustang");
        when(car.getList()).thenReturn(carTest);
        when(car.printDelegate(print)).thenReturn(9);
    }

    @Test
    public void test() {

        int no=dealer.show(car);
        assertEquals(2,number);//not worried about assert as of now

    }
}

我想不出一個解決方案來模擬 Dealer 類中的打印對象.因為,我在 Test 類中模擬它,但它是在被測方法中創建的.我做了我的研究,但不能找到任何好的資源.

I couldn't figure out a solution to mock the print object inside the Dealer class.Since,I mock it in the Test class,but it gets created in the method under test.I did my research,but couldn't find any good resource.

我知道從這個方法中創建打印對象并注入對象是更好的方法,但我想測試代碼原樣,在方法內部創建打印對象.有什么辦法嗎這個

I know taking Print object creation out of this method and Injection the object is the better way,but I would like to test the code as it is ,with the print object being created inside the method.Is there any way to do this

推薦答案

如果你只是想mock car.printDelegate() 的返回值,那么mock 任何Print 實例來調用怎么樣?

If you just want to mock the return value of car.printDelegate(), how about mock any Print instance for the call?

when(car.printDelegate(org.mockito.Matchers.any(Print.class))).thenReturn(9);

順便說一句,我對您的以下代碼感到困惑:-

By the way, I'm confusing about your following code:-

List<String> list=new LinkedList<String>(); // allocate a empty list worth 
list=car.getList();                         // nothing but wasting memory.
...
return num; // no definition, do you mean printedLines?

這篇關于在測試中的方法內部創建的模擬對象的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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(模擬;使用列表調用驗證方法,忽略列表中元素的順序)
主站蜘蛛池模板: 欧美国产精品一区二区三区 | 在线播放国产一区二区三区 | 99热首页| 粉嫩粉嫩芽的虎白女18在线视频 | 欧美高清性xxxxhdvideosex | 国产成人精品一区二区三区在线 | 久久天天综合 | 91久久国产综合久久 | 女同久久 | 成人三级视频 | 国产精品视频网 | 国产亚洲一区二区在线观看 | 免费精品一区 | 91精品国产欧美一区二区 | 操人视频在线观看 | 2019天天操 | 久久精品高清视频 | 在线视频成人 | 欧美国产日韩一区二区三区 | 久久综合久久综合久久 | 黄色香蕉视频在线观看 | 中文字幕在线视频免费观看 | 国产免费让你躁在线视频 | 久久成人国产精品 | 中文字幕在线观看第一页 | 欧美一级二级三级视频 | 亚洲精品在线看 | 国产婷婷在线视频 | yiren22 亚洲综合| 国产欧美日韩一区二区三区在线 | 91性高湖久久久久久久久_久久99 | 欧美日韩中文字幕在线 | a久久 | 男人久久天堂 | 一级欧美一级日韩片免费观看 | 色妞av | 日韩精品一区二区三区中文在线 | 日韩精品久久一区二区三区 | 亚洲精品在线视频 | 性欧美精品一区二区三区在线播放 | 久久伊人久久 |