【发布时间】:2011-07-04 10:44:20
【问题描述】:
首先推荐this问题。但似乎我的上下文不同。
我会尽量简明扼要。 (只是我发布的代码很大;)
我有大约 50 多个服务课程。并且需要为所有这些编写单元测试用例。 在所有这些测试类中,一些测试是常见的。 (删除、查找等)只是对象类型在服务类中会有所不同。
以下示例将清除图片。
考虑以下具有 CRUD 操作的服务类。
public class ObjService {
public Obj addObj(ParamType param, String var) { ... }
public void deleteObj(ParamType param, String var) { ... }
public List<Obj> findAllObj(ParamType param, String var) { ... }
public Obj findById(ParamType param, String var, String objIdToFind) { .. }
public List<Obj> getAllObjs(ParamType param, String var, ObjQuery objQuery) throws Exception { ... }
public Obj updateObj(ParamType param,
String var, Obj objToUpdate) throws Exception { }
}
现在我正在为 ObjService 类编写一个测试用例。 (测试框架 - testNG)
public class ObjServiceTest {
//These methods which will differ across all service classes
@Test
public void testAddObj() throws Exception {
addObj();
}
@Test
public void testUpdateObj() throws Exception {
Obj objToUpdate = addObj();
Obj updatedObj = updateObj(objToUpdate);
}
public Obj addObj() throws Exception {
//add obj test data and return the obj object
}
public Obj updateObj(Obj objToUpdate) throws Exception {
//update obj test data and return the updated obj object
}
//Following methods will be common to all classes. Except the name 'obj'
//e.g. For obj2 it would change to testDeleteObj2() { Obj2 obj2Todelete.... etc}
@Test
public void testDeleteObj() throws Exception {
Obj objToDelete = addObj();
deleteObj(objToDelete);
}
public void deleteObj(Obj objToDelete) throws Exception {
//delete the obj object
}
@Test
public void testFindById() throws Exception {
ObjService client = new ObjService();
List<Obj> objs = dsClient.findAllObj(...);
}
@Test
public void testFindAllObjs() throws Exception {}
@Test
public void testGetObjs() throws Exception {}
}
现在。为所有类手动编写通用方法肯定是一项耗时的工作。那么可以通过一些自动化来减少它吗?
(尽我最大的努力以至少令人困惑的方式提出问题)
编辑: 1) 测试类已经继承了包含所需初始设置的 BaseTestClass。所以这是个问题。
2) 请不要忘记部分,其中 整个过程都需要重构 方法不同。
【问题讨论】:
标签: java unit-testing testing automation automated-tests