【发布时间】:2011-05-28 11:17:30
【问题描述】:
假设我有以下代码,无法修改:
class A {
public doSomething() {
// lots of business logic
data = obtainData();
// lots of other business logic which depends on data
}
private obtainData() {
// connect to the database
// send web requests
// send manned missions to the moon
// process the obtained data
return data;
}
}
测试此类代码的最佳做法是什么?我想确保doSomething() 做它应该做的事情,但我想为它提供已知数据,而不是在obtainData() 中运行代码。
【问题讨论】:
-
私人而不是受保护...哎哟。 :-(
-
@Denis:私人有什么问题?
-
@zerkms:如果它受到保护,他可以声明一个扩展 A 的类 B 并重新定义 gainData()。根据他使用的语言,他可能无法使用私有方法来做到这一点。
-
私有没问题,很多变量最好设置为不可触碰。
-
我是亲私人的。只要有依赖注入。
标签: unit-testing testing