領測軟件測試網
Abstract
Factory 是另一種被普遍運用的創建型模式,Abstract Factory 通過專門的 Factory Class
來封裝對象創建的職責,并通過實現 Abstract Factory 來完成不同的創建邏輯。如果被測試單元所使用的外部對象是通過
Abstract Factory 創建的,則實現一個新的 Concrete Factory,并在此 Factory 中創建 Mock
Objects 是一個比較好的解決辦法。對于 Factory 本身,則可以在 setUp 測試的時候指定新的 Concrete Factory
;此外,借助依賴注入框架(如 Spring 的 BeanFactory),通過依賴注入的方式將 Factory
注入也是一種不錯的解決方案。對于簡單的依賴注入需求,可以考慮實現一個應用專有的依賴注入模塊,或者實現一個簡單的實現加載器,即根據配置文件載入相應
的實現,從而無需修改應用代碼,僅通過修改配置文件即可載入不同的實現,進而方便地修改程序的運行路徑,執行單元測試。
下面的代碼實現了一個簡單的 InstanceFactory:
// refer to http://www.opensc-project.org/opensc-java/export/100/trunk/ // pkcs15/src/main/java/org/opensc/pkcs15/asn1/InstanceFactory.java packagecom.instancefactory.demo;
importjava.lang.reflect.InvocationTargetException; importjava.lang.reflect.Method; importjava.lang.reflect.Modifier;
public class InstanceFactory { private final Method getInstanceMethod; public InstanceFactory(String type) { Class clazz =null; try { clazz = Class.forName(type); this.getInstanceMethod = clazz.getMethod("getInstance"); if(!Modifier.isStatic(this.getInstanceMethod.getModifiers()) || !Modifier.isPublic(this.getInstanceMethod.getModifiers())) throw new IllegalArgumentException( "Method [" + clazz.getName() + ".getInstance(Object)] is not static public."); } catch (NoSuchMethodException e) { throw new IllegalArgumentException( "Class [" + clazz.getName() + "] has no static getInstance(Object) method.", e); } catch (ClassNotFoundException e) { throw new IllegalArgumentException("Class [" + type + "] is not found"); } }
public Object getInstance() { try{ return this.getInstanceMethod.invoke(null); } catch (InvocationTargetException e) { if( e.getCause() instanceof RuntimeException ) throw (RuntimeException)e.getCause(); throw new IllegalArgumentException( "Method [" +this.getInstanceMethod + "] has thrown an checked exception.", e); } catch( IllegalAccessException e) { throw new IllegalArgumentException( "Illegal access to method [" +this.getInstanceMethod + "].", e); } } public Method getGetInstanceMethod() { return this.getInstanceMethod; } } |
以下代碼演示了 InstanceFactory 的簡單使用:
// BaseObjects.java package com.instancefactory.demo;
public interface BaseObjects { voidfunc(); }
// OuterObjects.java
package com.instancefactory.demo;
public class OuterObjects implements BaseObjects { public static BaseObjects getInstance() { return new OuterObjects(); } public void func() { System.out.println("OuterObjects.func"); } }
// MockOuterObjects.java package com.instancefactory.demo; public class MockOuterObjects implements BaseObjects { public static BaseObjects getInstance() { return new MockOuterObjects(); } public void func() { System.out.println("MockOuterObjects.func"); } }
// 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/