1

我有一个在他有生之年调用 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.*

我真的不明白发生了什么。我想我在某个地方完全错了。

4

1 回答 1

2

@PrepareForTest注释应该引用包含要测试的私有方法的类,这里MyRunnable请参阅https://code.google.com/p/powermock/wiki/MockitoUsage13的最后一个示例。

于 2013-08-26T14:25:24.510 回答