我有方法getAllCustomers
裏面的CustomerService
類。在這個方法裏面,我調用另一個從CustomerDao
類的靜態方法。 現在,當我正在寫方法getAllCustomers
在customerService
類,我想嘲笑 靜態方法的CustomerDao
,即getAllCustomers
的聯繫。以下是 CustomerService
類中方法getAllCustomers
的簡要代碼片段。 是否可以使用單元模擬靜態方法調用?如何使用UnitilsJUnit4模擬靜態方法?
Public static List<CustomerDate> getAllCustomers()
{
//some operations
List<CustomerDate> customers=CustomerDao.getAllCustomers();// static method inside CustomerDao
//some operations
}
上面的代碼只是我試圖把一個例子。請避免討論爲什麼這些方法設計爲靜態 方法。這是一個單獨的故事。)
這是一個很好的例子,爲什麼不在TDD環境中使用靜態方法 – Scorpion