假设我要测试这个ClassA
依赖于难以实例化的Java ClassB
:
public class ClassA
{
public ClassA()
{
String configFile = "config_file.xml";
// I have to pass configFile to instantiate ClassB.
// And for example if configFile does not exists in the testing machine?
// Wouldn't it be easier to have an empty constructor for classB to test ClassA?
ClassB classB = new ClassB(configFile);
}
// ...
}
ClassB
:
class ClassB
{
ClassB(String configFile)
{
// Set up configs.
}
// ...
}
classB
仅出于测试目的在内部创建一个空的构造函数是不好的做法吗?还是为此重写一个简化的模拟更好ClassB
?