org.mockito.exceptions.misusing.MissingMethodInvocationException: when() requires an argument which has to be 'a method call on a mock'

异常原因:

1.mockito的jar包中缺少方法

2.mock方法的时候,返回的是对象,而对象没有重写equals方法

3.mock的实例方法调用方法错误

解决方法:

1.用powermock中的api解决问题,在类中添加:@RunWith(PowerMockRunner.class)

2.如果是第二种情况,则需要重写返回对象的equals方法

3.

A a = new A();

PowerMockito.when(a.getTemplate()).thenReturn(template);

改成:

@Mock
private A a= spy(new A());

PowerMockito.when(a.getTemplate()).thenReturn(template);

posted @ 2017-03-01 09:53  ppjj  阅读(11023)  评论(0编辑  收藏  举报