Java Mockit:如何在 JMockit 中模拟通用 class 方法

Java Mockit : How to Mock a generic class method in JMockit

嗨,我有以下 classes

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 模拟模板(通用)class?

(我只能真正回答Mockito,因为这是我最熟悉的;但同样的原则应该适用于其他模拟框架)。

首先,您需要能够将 DataAccessLayer<UtilClass> 注入 ServiceLayer,例如

class ServiceLayer {
  private final DataAccessLayer<UtilClass> dal;

  ServiceLayer(DataAccessLayer<UtilClass> dal) {
    this.dal = dal;
  }

  public Integer testingMethode() {
    UtilClass info = dal.getData(UtilClass.class, 1);
    return info.getSomeFieldWithIntegerValue();
  }
}

这打破了通过使用 new.

创建的与 DataAccessLayer<UtilClass> 的静态耦合

现在,您可以通过创建非泛型子类来创建 DataAccessLayer<UtilClass> 的模拟实例:

class UtilClassDataAccessLayer extends DataAccessLayer<UtilClass> {}

然后创建一个模拟实例:

DataAccessLayer<UtilClass> mocked = mock(UtilClassDataAccessLayer.class);

现在,您可以根据需要配置此模拟,并将其传递到 ServiceLayer:

ServiceLayer serviceLayer = new ServiceLayer(mocked);

在 JMockit 中,实际上不需要在 ServiceLayer class 中创建一个保持变量,也不需要为 DataLayer 创建一个参数化的子 class ].以下测试工作正常:

package com.example.dsohl;

import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;

import org.junit.Test;
import org.junit.runner.RunWith;

import mockit.Deencapsulation;
import mockit.Expectations;
import mockit.Mocked;
import mockit.Tested;
import mockit.integration.junit4.JMockit;

@RunWith(JMockit.class)
public class TestTest {

    public static class UtilClass {
        public Integer foo() {
            return 5;
        }
    }

    public static class DataLayer<T> {
        public T getItem(Class<T> clazz, int itemId) {
            return null;
        }
    }

    public static class ServiceLayer {
        public Integer testMethod() {
            UtilClass util = new DataLayer<UtilClass>().getItem(UtilClass.class, 1);
            return util.foo();
        }
    }

// Test really begins here
    @Tested ServiceLayer svc;
    @Mocked DataLayer<UtilClass> data;
    @Mocked UtilClass util;

    @Test
    public void testDateSubtraction() throws Exception {
        new Expectations() {
            {
                new DataLayer<UtilClass>(); result = data;
                onInstance(data).getItem(UtilClass.class, 1); result = util;
                util.foo(); result = 37;
            }
        };

        Integer i = svc.testMethod();
        assertThat(i, equalTo(37));
    }

}

一些注意事项:首先,我的 DataLayer.getItem() returns 为 null,因此如果注入失败,我们会得到一个 NullPointerException,非常明显。显然你的代码不会像这样工作;这只是为了说服你。

其次,我使用 onInstance() 以便我们可以 100% 确定 DataLayer 构造函数的结果就是我们在接下来的测试步骤中使用的结果。 Expectations@Mocked 对象上的默认行为是记录对该 class 的任何对象的期望;这就是我们确定正在使用的是 我们的 对象的方式。 (通常我自己不担心这个,但是在使用 new 时我喜欢确定。)

最后,我省略了在这种情况下我可能会做的一些其他事情,比如使用 Verifications 块等。只是尽量简单明了。

尽情享受吧!

通用 class 可以像非通用一样被模拟:

@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);
}