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

在測(cè)試中的方法內(nèi)部創(chuàng)建的模擬對(duì)象

Mocking Objects Created Inside method Under test(在測(cè)試中的方法內(nèi)部創(chuàng)建的模擬對(duì)象)
本文介紹了在測(cè)試中的方法內(nèi)部創(chuàng)建的模擬對(duì)象的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我有一個(gè)我想測(cè)試的類.只要有可能,我就會(huì)對(duì)依賴于其他類對(duì)象的類進(jìn)行依賴注入.但是,我遇到了一個(gè)案例,我想在不重組對(duì)象的情況下模擬對(duì)象代碼而不是應(yīng)用 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.

這是被測(cè)試的類:

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;
    }
}

我的測(cè)試類是:

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

    }
}

我想不出一個(gè)解決方案來(lái)模擬 Dealer 類中的打印對(duì)象.因?yàn)?,我?Test 類中模擬它,但它是在被測(cè)方法中創(chuàng)建的.我做了我的研究,但不能找到任何好的資源.

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.

我知道從這個(gè)方法中創(chuàng)建打印對(duì)象并注入對(duì)象是更好的方法,但我想測(cè)試代碼原樣,在方法內(nèi)部創(chuàng)建打印對(duì)象.有什么辦法嗎這個(gè)

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 實(shí)例來(lái)調(diào)用怎么樣?

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);

順便說(shuō)一句,我對(duì)您的以下代碼感到困惑:-

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?

這篇關(guān)于在測(cè)試中的方法內(nèi)部創(chuàng)建的模擬對(duì)象的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(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)證方法,忽略列表中元素的順序)
主站蜘蛛池模板: av在线视| 国产清纯白嫩初高生在线播放视频 | 伊人伊人网 | 综合久久久久久久 | 久久久无码精品亚洲日韩按摩 | 欧美成人h版在线观看 | 狠狠干五月天 | 成人久久18免费网站图片 | 97免费视频在线观看 | 久久69精品久久久久久国产越南 | 日韩有码一区 | 成人精品一区二区三区中文字幕 | 欧美视频成人 | 国产成人精品久久二区二区 | 中文字幕影院 | 日韩在线 | 91久久国产综合久久91精品网站 | 亚洲一区视频在线 | 99reav| 国产精品一区二区av | 国产欧美精品一区二区三区 | 综合色影院 | 国产成人精品午夜视频免费 | 日韩中文字幕高清 | 三级黄片毛片 | 亚洲天堂免费 | 亚洲成人中文字幕 | 精品91久久 | 一本久久a久久精品亚洲 | 久久宗合色 | 中文字幕亚洲免费 | 91麻豆精品国产91久久久久久 | 日韩福利片| 在线视频成人 | 91精品国产高清一区二区三区 | 欧美午夜精品 | 国产 欧美 日韩 一区 | 免费看欧美一级片 | 国产成人免费视频网站高清观看视频 | 国产精品一区三区 | 亚洲精品视频一区 |