java Mockito:类的所有实例的模拟实例方法

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

Mockito: mock instance method for all instances of a class

javajunitmockito

提问by ddolce

I'm trying to stub an instance method of a particular class, so that when any instance of this Fooclass calls this instance method doSomething, the same objectis returned (see code below). However, mockito doesn't allow any matchers outside of verification or stubbing.

我正在尝试存根特定类的实例方法,以便当此类的任何实例Foo调用此实例方法时doSomethingobject都会返回相同的方法(请参见下面的代码)。但是,mockito 不允许验证或存根之外的任何匹配器。

Bar object = new Bar();
given(any(Foo.class).doSomething(Arg.class)).willReturn(object);

And in Foo.class:

并在Foo.class

Bar doSomething(Arg param) {
    Bar bar = new Bar();
    // Do something with bar
    return bar;
}

Any way I can achieve this goal with Mockito? Thanks!

我可以通过 Mockito 实现这个目标吗?谢谢!

采纳答案by Indra Basak

You should use PowerMockif you want Footo return the same instance of Barwhen you call doSomethingmethod on any instance of Foo. Here's an example:

如果您想在任何实例上调用方法时返回相同的实例,则应该使用PowerMock。下面是一个例子:FooBardoSomethingFoo

@RunWith(PowerMockRunner.class)
@PrepareForTest(Foo.class)
public class FooMockAllInstanceTest {

    @Test
    public void testMockInstanceofObjectCreation() throws Exception {
        Bar mockBar = PowerMockito.mock(Bar.class);
        when(mockBar.sayHello()).thenReturn("Hi John!");
        PowerMockito.whenNew(Bar.class)
                .withNoArguments()
                .thenReturn(mockBar);

        Foo myFooOne = new Foo();
        assertEquals(mockBar,  myFooOne.doSomething("Jane"));

        Foo myFooTwo = new Foo();
        assertEquals(mockBar,  myFooTwo.doSomething("Sarah"));

        Baz bazOne = new Baz();
        assertEquals(mockBar, bazOne.doSomething("Sam"));

        Baz bazTwo = new Baz();
        assertEquals(mockBar, bazTwo.doSomething("Nina"));
    }
}

This example will return the same Barobject even when Bazis called. Here's the Bazclass,

Bar即使Baz被调用,此示例也将返回相同的对象。下面是Baz班级

public class Baz {

    public Bar doSomething(String name) {
        Foo foo = new Foo();
        return foo.doSomething(name);
    }
}

Update 2

更新 2

There's another slightly better way to test with PowerMock. Here it's,

还有另一种稍微好一点的方法来使用 PowerMock 进行测试。来了,

@Test
public void testStubbingMethod() throws Exception {
    Bar mockBar = PowerMockito.mock(Bar.class);
    when(mockBar.sayHello()).thenReturn("Hi John!");

    PowerMockito.stub(PowerMockito.method(Foo.class, "doSomething",
            String.class)).toReturn(mockBar);

    Foo myFooOne = new Foo();
    assertEquals(mockBar, myFooOne.doSomething("Jane"));

    Foo myFooTwo = new Foo();
    assertEquals(mockBar, myFooTwo.doSomething("Sarah"));

    Baz bazOne = new Baz();
    assertEquals(mockBar, bazOne.doSomething("Sam"));

    Baz bazTwo = new Baz();
    assertEquals(mockBar, bazTwo.doSomething("Nina"));
}