首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Mockito MissingMethodInvocationException

Mockito MissingMethodInvocationException
EN

Stack Overflow用户
提问于 2016-04-26 12:46:53
回答 1查看 18.4K关注 0票数 3

我知道我的问题有很多类似的问题/答案,但我仍然无法用我的测试来解决这个问题。

问题:,我试图模拟Settings类,但是Mockito抱怨这一行:

代码语言:javascript
运行
复制
when(settings.settingsBuilder().put(new String("test"), "test").build()).thenReturn(settings)

MissiongMethodInvocationException:

代码语言:javascript
运行
复制
org.mockito.exceptions.misusing.MissingMethodInvocationException: 
when() requires an argument which has to be 'a method call on a mock'.
For example:
 when(mock.getArticles()).thenReturn(articles);

Also, this error might show up because:
1. you stub either of: final/private/equals()/hashCode() methods.
 Those methods *cannot* be stubbed/verified.
 Mocking methods declared on non-public parent classes is not supported.
2. inside when() you don't call method on mock but on some other object.

我尝试过许多可能的方法,但没有结果。贝娄是实际的测试方法。

代码语言:javascript
运行
复制
@RunWith(PowerMockRunner.class)
@PrepareForTest({Settings.class, Client.class})
public class AddressMatcherElasticTest {

private final AddressWebConfiguration configuration = mock(AddressWebConfiguration.class);
private final Settings settings = mock(Settings.class);
private final Client client = mock(Client.class);

  @Test
  public void match() throws Exception {
   when(configuration.getClusterName()).thenReturn(new String("name"));
   when(settings.settingsBuilder().put(new String("name"), "test").build()).thenReturn(settings);
   AddressMatcherElastic test = new AddressMatcherElastic(configuration);
   verify(configuration, times(1)).getClusterName();
 }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-26 12:52:07

settings是您的模拟,如果调用它,您可以告诉Mockito返回什么。

然而,settings.settingsBuilder()不返回一个模拟,这就是Mockito所抱怨的。当调用settingsBuilder时,您可以告诉Mockito返回一个模拟对象。

代码语言:javascript
运行
复制
Builder settingsBuilder = mock(Builder.class);
when(settingsBuilder.doX()).thenReturn(...);
when(settings.settingsBuilder()).thenReturn(settingsBuilder);

如果您不声明否则,则在默认情况下对对象引用返回null,请注意。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36865412

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档