Mockito:类的所有示例的模拟示例方法

ruarlubt  于 2023-03-08  发布在  其他
关注(0)|答案(2)|浏览(178)

我尝试stub一个特定类的示例方法,这样当这个Foo类的任何示例调用这个示例方法doSomething时,都会返回相同的object(见下面的代码)。

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

Foo.class中:

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

有什么方法可以让我用Mockito实现这个目标吗?谢谢!

piztneat

piztneat1#

如果你想在Foo的任何示例上调用doSomething方法时,Foo返回相同的Bar示例,你应该使用PowerMock

@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"));
    }
}

这个例子将返回相同的Bar对象,即使调用了Baz

public class Baz {

    public Bar doSomething(String name) {
        Foo foo = new Foo();
        return foo.doSomething(name);
    }
}
    • 更新2**

还有一个稍微好一点的方法来测试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"));
}
xzabzqsa

xzabzqsa2#

我发现这个页面告诉"如何模拟新对象的构造"
https://github.com/powermock/powermock/wiki/MockConstructor#example

// Class to be tested and need mock File object

public class PersistenceManager {

    public boolean createDirectoryStructure(String directoryPath) {
        File directory = new File(directoryPath);

        if (directory.exists()) {
            throw new IllegalArgumentException("\"" + directoryPath + "\" already exists.");
        }

        return directory.mkdirs();
    }
}
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import java.io.File;

import static org.easymock.EasyMock.expect;
import static org.junit.Assert.assertTrue;
import static org.powermock.api.easymock.PowerMock.*;

@RunWith(PowerMockRunner.class)
@PrepareForTest( PersistenceManager.class )
public class PersistenceManagerTest {

    @Test
    public void testCreateDirectoryStructure_ok() throws Exception {
        final String path = "directoryPath";
        File fileMock = createMock(File.class);

        PersistenceManager tested = new PersistenceManager();

        expectNew(File.class, path).andReturn(fileMock);

        expect(fileMock.exists()).andReturn(false);
        expect(fileMock.mkdirs()).andReturn(true);

        replay(fileMock, File.class);

        assertTrue(tested.createDirectoryStructure(path));

        verify(fileMock, File.class);
    }
}

相关问题