字号: 小 中 大 |
推荐给好友
上一篇 |
下一篇
应用设计模式编写易于单元测试的代码
发布: 2009-12-24 14:09 |
作者: 不详 |
来源:
领测国际测试网采编 |
查看: 23次 | 进入软件测试论坛讨论
领测软件测试网
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");
文章来源于领测软件测试网 https://www.ltesting.net/
|