应用设计模式编写易于单元测试的代码[7] 单元测试工具
// LogicToBeTested.java
packagecom.instancefactory.demo;
public class LogicToBeTested {
public static final String PROPERTY_KEY= "BaseObjects";
public void doSomething() {
// load configuration file and read the implementation class name of BaseObjects
// read it from properties to simplify the demo
// actually, the property file reader can be implemented by InstanceFactory
String impl = System.getProperty(PROPERTY_KEY);
InstanceFactory factory = new InstanceFactory(impl);
BaseObjects b = (BaseObjects)factory.getInstance();
b.doSomething();
}
}
// LogicTest.java
packagecom.instancefactory.demo;
importjunit.framework.TestCase;
public class LogicTest extends TestCase {
LogicToBeTested c;
protected void setUp() {
// set the property file of class map to a file for MockObjects, omitted
// use System.setProperty to simplify the demo
System.setProperty(LogicToBeTested.PROPERTY_KEY,
"com.instancefactory.demo.MockOuterObjects");软件测试
c = new LogicToBeTested();
}
public void testDoSomething() {
c.doSomething();
}
}
文章来源于领测软件测试网 https://www.ltesting.net/