我有一个可运行的程序,在他的一生中调用一个无效的私有(private)方法。 我想使用 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
注释应引用包含要测试的私有(private)方法的类,此处为 MyRunnable
。请参阅 https://code.google.com/p/powermock/wiki/MockitoUsage13 的最后一个示例.
关于java - 使用mockito监视线程上的私有(private)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18445888/