久久久久久久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(模擬;使用列表調用驗證方法,忽略列表中元素的順序)
主站蜘蛛池模板: 九九热国产视频 | 国产高清在线 | 成人在线精品 | 国产在线视频网 | 91av在线免费 | 91精品国产一区二区三区蜜臀 | 亚洲一区网站 | 国产精品福利久久久 | 夜夜爽99久久国产综合精品女不卡 | 欧洲尺码日本国产精品 | 国产精品精品视频一区二区三区 | 一级毛片免费看 | 欧美日韩网站 | 国产视频久久久久 | 国产分类视频 | 91免费观看国产 | 99精品一区二区 | 国产精品不卡一区 | 国产一区二区三区精品久久久 | 亚洲精品久久久久久久不卡四虎 | 亚洲一区二区三区四区五区午夜 | 人人鲁人人莫人人爱精品 | 亚洲成人在线视频播放 | 一二区成人影院电影网 | 伊人春色成人 | 久久亚洲一区二区三区四区 | 羞羞视频在线网站观看 | 天天操夜夜看 | 国产精品观看 | 九一在线 | 国产一级大片 | 亚洲欧美日韩在线 | 羞羞视频在线观看网站 | 999免费观看视频 | 91精品国产综合久久久密闭 | 国产乱码精品一区二区三区忘忧草 | 久久综合一区二区 | 狠狠干av| 欧美日韩国产免费 | 精品久久久久一区 | 久久综合久久综合久久综合 |