如何模拟方法与无效返回类型?

我实现了一个观察者模式,但我不能用Mockito模拟它,因为我不知道怎么做。

我试图在网上找到一个例子,但没有成功。

我的类是这样的:

public class World {

    List<Listener> listeners;

    void addListener(Listener item) {
        listeners.add(item);
    }

    void doAction(Action goal,Object obj) {
        setState("i received");
        goal.doAction(obj);
        setState("i finished");
    }

    private string state;
    //setter getter state
} 

public class WorldTest implements Listener {

    @Test public void word{
    World  w= mock(World.class);
    w.addListener(this);
    ...
    ...

    }
}

interface Listener {
    void doAction();
}

系统不会被mock触发。

我想展示上述系统的状态。并据此断言。


看一下Mockito API文档。正如链接文档中提到的(第12点),你可以使用Mockito框架中的doThrow()、doAnswer()、doNothing()、doReturn()方法家族中的任何一个来模拟void方法。

例如,

Mockito.doThrow(new Exception()).when(instance).methodName();

或者如果你想把它和后续行为结合起来,

Mockito.doThrow(new Exception()).doNothing().when(instance).methodName();

假设您正在下面的类World中模拟setter setState(String s),代码使用doAnswer方法来模拟setState。

World mockWorld = mock(World.class); 
doAnswer(new Answer<Void>() {
    public Void answer(InvocationOnMock invocation) {
      Object[] args = invocation.getArguments();
      System.out.println("called with arguments: " + Arrays.toString(args));
      return null;
    }
}).when(mockWorld).setState(anyString());

所谓的问题的解决方案是使用一个间谍Mockito.spy(…)而不是一个mock Mockito.mock(..)。

间谍使我们能够局部嘲笑。Mockito擅长这件事。因为您有一个不完整的类,所以您可以通过这种方式模拟该类中一些必需的位置。

为这一堆问题添加另一个答案(没有双关语的意思)……

你需要调用doAnswer方法,如果你不能\不想使用间谍的。然而,你并不一定需要自己给出答案。有几个默认实现。值得注意的是,CallsRealMethods。

在实践中,它看起来像这样:

doAnswer(new CallsRealMethods()).when(mock)
        .voidMethod(any(SomeParamClass.class));

Or:

doAnswer(Answers.CALLS_REAL_METHODS.get()).when(mock)
        .voidMethod(any(SomeParamClass.class));

我想我已经找到了这个问题的一个更简单的答案,为一个方法调用真正的方法(即使它有一个void返回),你可以这样做:

Mockito.doCallRealMethod().when(<objectInstance>).<method>();
<objectInstance>.<method>();

或者,你可以为该类的所有方法调用real方法,这样做:

<Object> <objectInstance> = mock(<Object>.class, Mockito.CALLS_REAL_METHODS);

我认为你的问题是由于你的测试结构。我发现很难将模拟与在测试类中实现接口的传统方法混合在一起(就像您在这里所做的那样)。

如果将侦听器实现为Mock,则可以验证交互。

Listener listener = mock(Listener.class);
w.addListener(listener);
world.doAction(..);
verify(listener).doAction();

这应该让你满意,“世界”正在做正确的事情。

加上@sateesh所说的,当你只是想模拟一个void方法以防止测试调用它时,你可以这样使用Spy:

World world = new World();
World spy = Mockito.spy(world);
Mockito.doNothing().when(spy).methodToMock();

当您希望运行测试时,请确保在spy对象上调用in test方法,而不是在world对象上调用。例如:

assertEquals(0, spy.methodToTestThatShouldReturnZero());

首先:你应该总是导入mockito static,这样代码将更易于阅读(和直观):

import static org.mockito.Mockito.*;

对于部分嘲笑,仍然保持原始功能上的休息模拟提供“间谍”。

你可以这样使用它:

private World world = spy(new World());

要消除一个正在执行的方法,你可以使用这样的方法:

doNothing().when(someObject).someMethod(anyObject());

给一个方法一些自定义行为,使用"when"和"thenReturn":

doReturn("something").when(this.world).someMethod(anyObject());

对于更多的例子,请在文档中找到优秀的模拟样品。

如何用mockito模拟void方法-有两个选项:

doAnswer -如果我们想让我们的mock void方法做一些事情(尽管它是void,但仍然模拟该行为)。 doThrow——如果你想从mock void方法抛出一个异常,那么还有Mockito.doThrow()。

下面是如何使用它的示例(不是理想的用例,只是想说明基本用法)。

@Test
public void testUpdate() {

    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            Object[] arguments = invocation.getArguments();
            if (arguments != null && arguments.length > 1 && arguments[0] != null && arguments[1] != null) {

                Customer customer = (Customer) arguments[0];
                String email = (String) arguments[1];
                customer.setEmail(email);

            }
            return null;
        }
    }).when(daoMock).updateEmail(any(Customer.class), any(String.class));

    // calling the method under test
    Customer customer = service.changeEmail("old@test.com", "new@test.com");

    //some asserts
    assertThat(customer, is(notNullValue()));
    assertThat(customer.getEmail(), is(equalTo("new@test.com")));

}

@Test(expected = RuntimeException.class)
public void testUpdate_throwsException() {

    doThrow(RuntimeException.class).when(daoMock).updateEmail(any(Customer.class), any(String.class));

    // calling the method under test
    Customer customer = service.changeEmail("old@test.com", "new@test.com");

}
}

你可以在我的文章how to mock with Mockito中找到更多关于如何用Mockito模拟和测试void方法的细节(一个包含示例的全面指南)

在Java 8中,假设你有一个org.mockito.Mockito.doAnswer的静态导入,这可以做得更清楚一些:

doAnswer(i -> {
  // Do stuff with i.getArguments() here
  return null;
}).when(*mock*).*method*(*methodArguments*);

返回null;是重要的,如果没有它,编译将失败,并出现一些相当模糊的错误,因为它将无法为doAnswer找到合适的覆盖。

例如,ExecutorService可以立即执行传递给execute()的任何可运行对象,可以使用以下方法实现:

doAnswer(i -> {
  ((Runnable) i.getArguments()[0]).run();
  return null;
}).when(executor).execute(any());

在您的示例中,您应该模拟Listener项并使用Mockito。验证以检查与它的交互

如果你需要在mock void方法中做一些操作,你需要操纵发送给void方法的参数;你可以结合Mockito。doAnswer与ArgumentCaptor。捕捉方法。

假设你有一个自动连接GalaxyService的SpaceService,它有一个叫做someServiceMethod的void方法。

您需要为SpaceService中的一个方法编写测试,该方法调用了GalaxyService的void方法。你的星球也是在宇宙服务内部生成的。所以你没有机会嘲笑我。

下面是您希望为其编写测试的示例SpaceService类。

class SpaceService {
    @Autowired
    private GalaxyService galaxyService;

    public Date someCoolSpaceServiceMethod() {
        // does something

        Planet planet = new World();
        galaxyService.someServiceMethod(planet); //Planet updated in this method.

        return planet.getCurrentTime();
    }
}

GalaxyService。someServiceMethod方法需要一个行星参数。在方法中做一些事情。看到的:

GalaxyService {
    public void someServiceMethod(Planet planet) {
        //do fancy stuff here. about solar system etc.

        planet.setTime(someCalculatedTime); // the thing that we want to test.

        // some more stuff.
    }
}

你想测试这个功能。

这里有一个例子:

ArgumentCaptor<World> worldCaptor = ArgumentCaptor.forClass(World.class);
Date testDate = new Date();

Mockito.doAnswer(mocked-> {
    World capturedWorld = worldCaptor.getValue();
    world.updateTime(testDate);
    return null;
}).when(galaxyService.someServiceMethod(worldCaptor.capture());

Date result = spaceService.someCoolSpaceServiceMethod();

assertEquals(result, testDate);