2014-03-01 40 views
1

我需要編寫測試用例類似於以下類的類:如何使用Powermockito讓私人無效的方法跳過

public class TestClass { 

public String loadBean(int i) { 
    String s = null; 
    if (i <= 0) { 
     s = "less"; 
     // call some webservice that we don't need to test 
     callWebService(s); 
    } else { 
     s = "more"; 
    } 
    return s; 
} 

private void callWebService(String s) { 
    //Some complex code here 

} 

我需要測試loadBean唯一方法,問題是 - 我無法使用PowerMock跳過方法調用來調用WebService()。因此,爲了更清楚地解釋我需要一個調用loadBean的測試用例,但在遇到調用WebService方法繼續執行流程時什麼也不做。

我在同一個網站上閱讀過它,我們可以用PowerMockito做到這一點,但無法讓它正常工作。

請幫忙。

回答

0

這爲我工作:

import static org.powermock.api.mockito.PowerMockito.when; 
import static org.powermock.api.mockito.PowerMockito.doNothing; 

//Skipping rest of the code 
TestClass testSpy = PowerMockito.spy(new TestClass()); 
doNothing().when(testSpy, method(TestClass.class, "callIt")).withNoArguments(); 

上面的代碼跳過我的課的私人無效CALLIT()方法。

+0

「方法(TestClass.class,」callIt「)」中的方法是什麼? –

+0

我得到一個未完成的存根異常錯誤的相同 –