[英]Spy private method on thread with mockito
我有一个可运行对象,在他的一生中都调用了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的最后一个示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.