2014-12-05 59 views
0

我試圖嘲笑使用JMockit的私人方法,並掙扎。我一直在研究這些教程,可以模擬那些返回值不是沒有的值的私有方法。這種特殊的方法與數據庫交互,不會返回任何東西。爲了這個測試的目的,我想要做的就是有效地屏蔽掉這個方法。我正在使用的測試格式如下所示,請注意,通常在調用方法解封裝後結果將是直接的。JMockit,我如何嘲笑沒有回報的私人方法

@Test 
public void testRetrieveAndSaveReport() { 

    //Make class with private class final, to be used in the Exceptionsinners class 
    final ESReportOutputLogic eSReportOutputLogic = new ESReportOutputLogic(); 

    // Define Expectations 
    // pass eSReportOutputLogic as argument to make it a Mocked type in the Exceptions Class 
    new Expectations(eSReportOutputLogic){ 
     ESReportOutputLogic eSReportOutputLogic; 
     { 
      Deepcapsulation.invoke(eSReportOutputLogic); 

     } 
    }; 

    ESReportOutputLogic rol = new ESReportOutputLogic(); 
    rol.retrieveAndSaveReport("","",1); 

    // asserts..... 
} 
+0

由於它是一個私有方法,你不能只是改變方法的簽名來返回一些值。在這種情況下,如果數據庫操作成功,則返回0,否則返回1.很難想象任何不將其活動結果傳遞給調用方法的私有方法。它可以通過返回值或拋出異常。 – Gaurav 2014-12-05 16:58:52

回答

1

您使用哪種抽象方式與數據庫交互?

我建議嘲笑這個,而不是試圖嘲笑從你的測試的角度來看should not exist

嘲諷私有方法意味着您實際上將代碼留在您的應用程序中,並且未被測試覆蓋。

+0

感謝您的輸入[rol.retrieveAndSaveReport(「」,「」,1)與數據庫交互]。當我第一次研究這個問題時,我得到了這個,現在我回到它,我可以想出更好的方法來解決我的問題。最簡單的就是使用System-rules API。這有方法ExpectedSystemExit()和expectSystemExitWithStatus(int)。任何想要使用它的人都會得到正確的JUnit版本才能使用它。 – 2015-10-23 13:41:18