【发布时间】:2016-07-11 20:26:37
【问题描述】:
您好,我有以下课程
public class DataAccessLayer<T> {
public T getData(Class<?> dataInfoType ,Integer id){
//Some logic here
}
}
public class ServiceLayer{
//this method has to be tested
public Integer testingMethode{
//The following line should be mocked
UtilClass info = new DataAccessLayer<UtilClass>().getData(UtilClass.class, 1);
retutn info.getSomeFieldWithIntegerValue();
}
}
我想为 testingMethode 编写测试用例,因为我需要在 DataAccessLayer<T> 中模拟 getData() 方法
jmockit 是否可以模拟 Template(Generic) 类?
【问题讨论】:
-
在 mockito 中模拟泛型类的典型方法是定义一个非泛型子类(例如
class UtilClassDataAccessLayer extends DataAccessLayer<UtilClass>),然后创建该类的模拟。你能在 jmockit 中做类似的事情吗? -
我刚试过但不适合我。你能发布一个示例 sn-p 吗?
标签: java unit-testing generics junit jmockit