繼續 你來寫,我來跑 的故事,隔天你上班了,測試人員紅著眼來找你(顯然睡眠不足),說他想要任意組合測試,例如CalculatorPlusTest與CalculatorMinusTest一起測,CalculatorDivideTest與CalculatorMultiplyTest一起測,CalculatorOOOTest、CalculatorXXXTest、CalculatorYYYTest...等一起測,總之聽來想在抱怨,你昨天給的執行器作不到這點。
你盤算了一下,自由組合,這有什麼難的,你的TestRunner可以給他更多組合的彈性,總之你先設計了一個TestSuite:
package test.cc.openhome;
import java.util.*;
public class TestSuite implements Test {
private List<Test> tests;
public TestSuite() {
tests = new ArrayList<Test>();
}
@Override
public void run() {
for (Test test : tests) {
test.run();
}
}
public void add(Test test) {
tests.add(test);
}
}
import java.util.*;
public class TestSuite implements Test {
private List<Test> tests;
public TestSuite() {
tests = new ArrayList<Test>();
}
@Override
public void run() {
for (Test test : tests) {
test.run();
}
}
public void add(Test test) {
tests.add(test);
}
}
接著重構了一下TestRunner:
package test.cc.openhome;
public class TestRunner {
public static void run(Test test) {
test.run();
}
}
public class TestRunner {
public static void run(Test test) {
test.run();
}
}
接著告訴測試人員要組合測試,可以使用TestSuite,例如CalculatorPlusTest與CalculatorMinusTest一起測:
package test.cc.openhome;
public class CalculatorPlusMinusTest {
public static Test suite() {
TestSuite suite = new TestSuite();
suite.add(new CalculatorPlusTest());
suite.add(new CalculatorMinusTest());
return suite;
}
public static void main(String[] args) {
TestRunner.run(suite());
}
}
public class CalculatorPlusMinusTest {
public static Test suite() {
TestSuite suite = new TestSuite();
suite.add(new CalculatorPlusTest());
suite.add(new CalculatorMinusTest());
return suite;
}
public static void main(String[] args) {
TestRunner.run(suite());
}
}
例如CalculatorDivideTest與CalculatorMultiplyTest一起測:
package test.cc.openhome;
public class CalculatorDivideMultiplyTest {
public static Test suite() {
TestSuite suite = new TestSuite();
suite.add(new CalculatorDivideTest());
suite.add(new CalculatorMultiplyTest());
return suite;
}
public static void main(String[] args) {
TestRunner.run(suite());
}
}
public class CalculatorDivideMultiplyTest {
public static Test suite() {
TestSuite suite = new TestSuite();
suite.add(new CalculatorDivideTest());
suite.add(new CalculatorMultiplyTest());
return suite;
}
public static void main(String[] args) {
TestRunner.run(suite());
}
}
或是CalculatorOOOTest、CalculatorXXXTest、CalculatorYYYTest...等一起測:
package test.cc.openhome;
public class CalculatorOOOXXXYYYTest {
public static Test suite() {
TestSuite suite = new TestSuite();
suite.add(new CalculatorOOOTest());
suite.add(new CalculatorXXXTest());
suite.add(new CalculatorYYYTest());
return suite;
}
public static void main(String[] args) {
TestRunner.run(suite());
}
}
public class CalculatorOOOXXXYYYTest {
public static Test suite() {
TestSuite suite = new TestSuite();
suite.add(new CalculatorOOOTest());
suite.add(new CalculatorXXXTest());
suite.add(new CalculatorYYYTest());
return suite;
}
public static void main(String[] args) {
TestRunner.run(suite());
}
}
對了,別忘了告訴測試人員,想全部組合在一起測也沒問題:
package test.cc.openhome;
public class CalculatorAllTest {
public static Test suite() {
TestSuite suite = new TestSuite();
suite.add(CalculatorPlusMinusTest.suite());
suite.add(CalculatorDivideMultiply.suite());
suite.add(CalculatorOOXXXYYYTest.suite());
return suite;
}
public static void main(String[] args) {
TestRunner.run(suite());
}
}
public class CalculatorAllTest {
public static Test suite() {
TestSuite suite = new TestSuite();
suite.add(CalculatorPlusMinusTest.suite());
suite.add(CalculatorDivideMultiply.suite());
suite.add(CalculatorOOXXXYYYTest.suite());
return suite;
}
public static void main(String[] args) {
TestRunner.run(suite());
}
}
想怎麼組合就怎麼組合...其實這是 Composite 模式 的實現....所以,今天你又可以早點下班了...
故事還沒完...