我試圖嘲笑使用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,否則返回1.很難想象任何不將其活動結果傳遞給調用方法的私有方法。它可以通過返回值或拋出異常。 – Gaurav 2014-12-05 16:58:52