您的位置:軟件測(cè)試 > 開(kāi)源軟件測(cè)試 > 開(kāi)源單元測(cè)試工具 > junit
Junit模板方法模式應(yīng)用
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時(shí)間:[ 2014/10/10 13:35:22 ] 推薦標(biāo)簽:Junit 測(cè)試 開(kāi)源軟件測(cè)試

  模板模式在junit3中的應(yīng)用
  查看TestCase.java源代碼
public abstract class TestCase extends Assert implements Test {
public void runBare() throws Throwable {
setUp();
try {
runTest();
}
finally {
tearDown();
}
}
protected void setUp() throws Exception {
}
protected void tearDown() throws Exception {
}
protected void runTest() throws Throwable {
assertNotNull(fName);
Method runMethod= null;
try {
runMethod= getClass().getMethod(fName, null);   //null表示測(cè)試方法必須是無(wú)參的
} catch (NoSuchMethodException e) {
fail("Method ""+fName+"" not found");
}
if (!Modifier.isPublic(runMethod.getModifiers())) {
fail("Method ""+fName+"" should be public");   //明確表示測(cè)試方法必須是public修飾的
}
runMethod.invoke(this, new Class[0]);
}
}
  在TestCase.java中的runBare()方法中定義了測(cè)試方法的執(zhí)行步驟:setUp()-->runTest()-->tearDown(); 具體方法的真正實(shí)現(xiàn)推遲到子類中去實(shí)現(xiàn)。
  junit3中引入模板方式模式的好處:
  1)將各個(gè)測(cè)試用例中的公共的行為(初始化信息和釋放資源等)被提取出來(lái),可以避免代碼的重復(fù),簡(jiǎn)化了測(cè)試人員的工作;
  2)在TestCase中實(shí)現(xiàn)一個(gè)算法的不變部分,并且將可變的行為留給子類來(lái)實(shí)現(xiàn)。增強(qiáng)了系統(tǒng)的靈活性。使JUnit框架僅負(fù)責(zé)算法的輪廓和骨架,而開(kāi)發(fā)人員則負(fù)責(zé)給出這個(gè)算法的各個(gè)邏輯步驟。

上一頁(yè)12下一頁(yè)
軟件測(cè)試工具 | 聯(lián)系我們 | 投訴建議 | 誠(chéng)聘英才 | 申請(qǐng)使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd