java 如何在单元测试中覆盖被测试类调用的方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37235373/
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-03 02:19:34  来源:igfitidea点击:

How to override a method in unit tests that is called from which the class being tested

javaunit-testingjunitmockingeasymock

提问by Khawar Ali

I am testing a class A's function func1.

我正在测试 A 类的函数 func1。

Func1 has a local variable of Class B and calls B's function func2. Code looks something like this:

Func1 有一个 B 类的局部变量,调用 B 的函数 func2。代码如下所示:

public Class A
{
    public func1()
    {
       B object = new B();
       int x = object.func2(something);
    }
}

When I am testing func1 in its unit tests, I don't want func2 to get called.

当我在单元测试中测试 func1 时,我不希望 func2 被调用。

So I am trying to do something like this in the test:

所以我试图在测试中做这样的事情:

B textObject = new B()
{
   @override
   int func2(something)
   {
      return 5;
   }
}

But it is still calling the func2 in the class B. Please suggest how to handle this.

但它仍然在调用 B 类中的 func2。请建议如何处理。

采纳答案by René Link

If you want to override the new B()constructor call - place it in an own method.

如果你想覆盖new B()构造函数调用 - 将它放在自己的方法中。

public Class A
{

    public func1()
    {
       B object = newB();
       int x = object.func2(something);
    }

    protected B newB(){
        return new B();
    }
}

In your test you can then override the Bconstructor call.

在您的测试中,您可以覆盖B构造函数调用。

public class APartitialMock extends A {

   protected B newB(){
      return new BMock();
   }
}

public class BMock extends B {

    int func2(something) {
        return 5
    }

}

Then use APartitialMockto test the func1with your kind of B.

然后使用APartitialMock来测试func1你的那种B

PSIf you can or want to use a framework take a look at powermock - Mock Constructor

PS如果您可以或想要使用框架,请查看powermock - Mock Constructor

回答by krokodilko

I can make B as a class variable in A but that doesn't seem to help either. What would you suggest in this case?

我可以将 B 作为 A 中的类变量,但这似乎也无济于事。在这种情况下,您有什么建议?

If you make B as a class variable, then you can mock B, and "swap" it in the tested object of A.
Maybe not very elegant, but quick and simple.

An example:

如果你把 B 作为一个类变量,那么你可以模拟 B,并在 A 的测试对象中“交换”它。
也许不是很优雅,但快速简单。

一个例子:

public class B {
    int func2(int something){
        return 3*something;
    }
}


public class A
{
    // declare it as protected - a test need to have access to this field to replace it
    protected B object = new B();

    public int func1()
    {
       int x = object.func2(22);
       return x;
    }
}


And a test:

还有一个测试:

import static org.mockito.Mockito.*;
import static org.junit.Assert.*;

import org.junit.Test;

public class ATest {

    @Test
    public void test() {
        A myA = new A();
        B myB = mock(B.class);

        // dummy function
        when(myB.func2(anyInt())).thenReturn(20);

        // replace B with mocked B
        myA.object = myB;

        int actual = myA.func1();

        assertEquals(20, actual);
    }
}