软件单元测试实践小结[2] 单元测试代码
相关的测试代码可以这样:
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);
}
}
public class MockDatabaseDao2 implements DatabaseDao2 {
private Map map;
public void setMap(Map map){
this.map = map;
}
public void delete(args){
map.put("MockDatabaseDao2.delete", args);
}
}
public class ServiceExampleTestCase{
private Map map = new HashMap();
public void testNoOutputMethod(){
DaoTest test = new DaoTest();
DatabaseDao1 dao1 = new MockDatabaseDao1(); 软件测试
dao1.setMap(map);
dao2.setMap(map);
DatabaseDao2 dao2 = new MockDatabaseDao2();
test.setDao1(dao1);
test.setDao2(dao2);
test.noOutputMethod();
assertEquals(new Boolean(true), new Boolean(map.containsKey("MockDatabaseDao1.update")));
assertEquals(new Boolean(true), new Boolean(map.containsKey("MockDatabaseDao2.delete")));
}
}
文章来源于领测软件测试网 https://www.ltesting.net/