首页 > 解决方案 > 我如何模拟 Ignition.start()

问题描述

由于各种原因,我的单元测试环境无法访问启动 ignite 所需的环境。我不需要 ignite 来开始测试,我希望代码忽略对 Ignition.start() 的调用。我怎么做?

我尝试过模拟点火,但是当我尝试模拟 start() 方法时,它会引发错误。

when(ignitionMock.start())

这会导致以下错误:

org.mockito.exceptions.misusing.MissingMethodInvocationException: when() 需要一个参数,该参数必须是“模拟的方法调用”。例如:when(mock.getArticles()).thenReturn(articles);

此外,此错误可能会出现,因为: 1. 您存根以下任一方法:final/private/equals()/hashCode() 方法。这些方法不能被存根/验证。不支持在非公共父类上声明的模拟方法。2. 在 when() 中,您不会在 mock 上调用方法,而是在其他对象上调用方法。

标签: javaunit-testingjunitmockingmockito

解决方案


你可以使用doNothing()

doNothing().when(ignitionMock).start();

推荐阅读