应用设计模式编写易于单元测试的代码[7]

发表于:2010-03-15来源:作者:点击数: 标签:代码单元应用模式编写
应用设计模式编写易于单元测试的代码[7] 单元测试工具 // LogicToBeTested. java packagecom.instancefactory.demo; public class LogicToBeTested { public static final String PROPERTY_KEY= "BaseObjects"; public void doSomething() { // load configu

       应用设计模式编写易于单元测试的代码[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.ltesting.net