java 在方法内部创建的模拟对象正在测试中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25069564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 07:15:56  来源:igfitidea点击:

Mocking Objects Created Inside method Under test

javajunitmockingmockitojunit4

提问by user3897395

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.

我有一个我想测试的类。只要有可能,我就会为那个依赖于其他类的对象的类进行依赖注入。但是,我遇到了一种情况,我想在不重构代码的情况下模拟对象而不是应用 DI。

Here is the class under test:

这是被测类:

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

and my test class for this is:

我的测试课程是:

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

    }
}

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.

我想不出在 Dealer 类中模拟打印对象的解决方案。因为,我在 Test 类中模拟它,但它是在被测方法中创建的。我做了我的研究,但找不到任何好处资源。

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

我知道从这个方法中创建打印对象并注入对象是更好的方法,但我想测试代码原样,在方法内部创建打印对象。有没有办法做到这一点

采纳答案by Tatera

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

如果您只是想模拟 car.printDelegate() 的返回值,那么模拟调用的任何 Print 实例如何?

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?