应用设计模式编写易于单元测试的代码[3] 单元测试工具
以下则是对应的 MockOuterObjects、MockFactory 以及单元测试的实现:
// MockOuterObjects.java
package com.factorymethod.demo;
public class MockOuterObjects implements BaseObjects {
public void func() {
System.out.println("MockOuterObjects.func");
}
}
// MockLogicToBeTested.java
package com.factorymethod.demo;
public class MockLogicToBeTested extends LogicToBeTested {
public BaseObjects createBase() {
return new MockOutterObjects();
}
}
// LogicTest.java
package com.factorymethod.demo;
import junit.framework.TestCase;
public class LogicTest extends TestCase {
LogicToBeTested c;
protected void setUp() {
c =new MockLogicToBeTested();
}
public void testDoSomething() {
c.doSomething();
}