字號: 小 中 大 |
推薦給好友
上一篇 |
下一篇
應用設計模式編寫易于單元測試的代碼
發布: 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");
文章來源于領測軟件測試網 http://www.kjueaiud.com/

老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月
|