應用設計模式編寫易于單元測試的代碼[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();
}
}
文章來源于領測軟件測試網 http://www.kjueaiud.com/