測試的關注點在于測試邏輯,只要有邏輯就要寫測試代碼。測試的手段就是驗證所有被測試方法的所有產出物,包括:
1. 測試方法的返回值
2. 測試方法的執行流程
例如:
public class DomainService {
private static TheDAO dao = new TheDAO ();
public ReturnObject findByCond(String) {
return (ReturnObject)dao.getBeanByCondition("select * from ReturnObject where cond="+ paramter, ReturnObject.class);
}
}
A.測傳遞給TheDAO.getBeanByCondition的參數的正確性,如果參數不是”select * from ReturnObject where cond=?”和ReturnObject.class則返回為null。
B.測返回的對象正確性。
特別是第二點,在商業應用上比較常見的。通常有些方法無明顯output,通常是執行寫表操作的。對于這樣的方法就是測試它的執行流程。當然這些方法本身包含邏輯的。
一個簡單的解決方法是利用Access Log來實現(雖然這樣的測試不多,而寫的case代碼也看著怪怪的)。
public class ServiceExample{
private DatabaseDao1 dao1;
private DatabaseDao2 dao2;
public void noOutputMethod(){
if(...)
dao1.update(...);
if(...)
dao2.delete();
}
}
相關的測試代碼可以這樣:
public class MockDatabaseDao1 implements DatabaseDao1 {
private Map map;
public void setMap(Map map){
this.map = map;
}
public void update(args){
map.put("MockDatabaseDao1.update", args);
}
}
文章來源于領測軟件測試網 http://www.kjueaiud.com/