PowerMockito无法间歇性地模拟最终方法

时间:2014-05-13 06:42:07

标签: java unit-testing mocking testng powermock

我正在使用TestNG 6.8.8,Mockito 1.9.5和PowerMock 1.5.4。当我模拟Child类并调用stubbed final方法时,测试有时会通过,并且有时会因错误MissingMethodInvocationException而失败。

这是PowerMock的错误吗?

public abstract class Parent implements Serializable {
  protected abstract void validate();

  public final Date getLastModified() {
    return lastModified;
  }
}

public class Child extends Parent {
  @Override
  protected void validate() {
    // nothing for now.
  }
}


import static org.powermock.api.mockito.PowerMockito.when;
@PrepareForTest({ Parent.class, Child.class })
public class ChildTest {
  @Test
  public final void testChildMethod() {
    Child childObj = PowerMockito.mock(Child.class);
    when(childObj.getLastModified()).thenReturn(new Date());

    TestCodeThatResultsInCallToChild.getLastModified();
  }
}

错误讯息:

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.
2. inside when() you don't call method on mock but on some other object.
3. the parent of the mocked class is not public.
   It is a limitation of the mock engine.
" type="org.mockito.exceptions.misusing.MissingMethodInvocationException

0 个答案:

没有答案
相关问题