领测软件测试网
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(); } } |
文章来源于领测软件测试网 https://www.ltesting.net/