应用设计模式编写易于单元测试的代码[6] 单元测试工具
以下代码演示了 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/