我有一个可运行程序,在他有生之年调用了一个void私有方法。我想使用PowerMockito测试一下,我的方法"processStep"实际上对每个参数只调用一次。
MyRunnable类
public class MyRunnable extends Runnable {
MyRunnable(args){
...
}
@Override
public final void run{
...
processStep();
...
}
private void processTep(a){
...
addAttributeResult();
...
}
private void addAttributeResult(){
...
}
}
我的测试类用于测试MyRunnable类
@PowerMockIgnore("org.apache.log4j.*")
@RunWith(PowerMockRunner.class)
@PrepareForTest({ DBReader.class, MyRunnable.class })
public class CycleManagerTest {
@Test
public void myTest(){
MyRunnable myRunnable = new MyRunnable(arg[] {a,b});
Thread t = new Thread(myRunnable);
t.start();
while (myRunnable.getNbrEndCycle() < 1) {
Thread.sleep(10);
}
t.interrupt();
for(String s : arg){
PowerMockito.verifyPrivate(myRunnable, times(1)).invoke("processStep", a);
}
}
}
当只有一个参数时,测试成功了,但当有很多参数时,则测试出现错误,如下所示:
*org.mockito.exceptions.misusing.UnfinishedVerificationException:
Missing method call for verify(mock) here:
-> at fr.MyRunnable.addAttributeResult(MyRunnable.java:254)
Example of correct verification:
verify(mock).doSomething()
Also, this error might show up because you verify either of: final/private/equals()/hashCode() methods.
Those methods *cannot* be stubbed/verified.*
我真的不明白发生了什么。我想我在某个地方完全错了。
@PrepareForTest
注释应该引用包含要测试的私有方法的类,此处为MyRunnable
。查看的最后一个示例https://code.google.com/p/powermock/wiki/MockitoUsage13.