簡體   English   中英

使用Mockito的線程上的間諜私有方法

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM