我有一个类接受一个参数,然后从参数中填充一个私有哈希图。
Public class Table {
private Map<String, String> map = new HashMap<String, String>();
private Workbook workbook;
// approach 1
Public Table(Workbook workbook) {
this.workbook = workbook;
// populate map using workbook
}
// approach 2
Public Table(InputStream is) {
this.workbook = WorkbookFactory.create(is)
// Populate map
}
// approach 3
Public Table(File file) {
this.workbook = WorkbookFactory.create(file)
// populate map
}
}
Workbook 是一个 Apache Poi 工作簿。考虑到junit和mockito的最佳方法是什么。
我倾向于方法 1,因为根据这个,http ://misko.hevery.com/code-reviewers-guide/flaw-constructor-does-real-work/我应该避免在构造函数中使用 new 关键字。但我不希望客户了解 Apache POI 才能使用此类。
方法 2 和 3 非常相似。有了这个,我可以传入文件或输入流,而不必担心底层实现。但是模拟会很困难,因为我不能用这种方法模拟工作簿?