嗨,我有以下课程

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编写测试用例,因为我需要在getData()中模拟DataAccessLayer<T>方法

jmockit是否可以模拟Template(Generic)类?

最佳答案

泛型类可以像非泛型类一样进行模拟:

@Test
public void example(@Mocked final DataAccessLayer<UtilClass> mock)
{
    final UtilClass data = new UtilClass(123);
    new Expectations() {{ mock.getData(UtilClass.class, 1); result = data; }};

    int result = new ServiceLayer().testingMethode();

    assertEquals(123, result);
}

07-24 18:59