使用 Mockito 通过反射模拟方法 [英] Using Mockito to mock methods by reflection

查看:81
本文介绍了使用 Mockito 通过反射模拟方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用 Mock-Factory 为我们的开发人员提供尽可能舒适的模拟功能,同时减少对 mockito 本身所需的专业知识.

We are using a Mock-Factory to give our developers the most possible comfort about mocking functionality with the less possible needed know-how about mockito itself.

为此,我们的 Mock-Factory 提供了一个方法来创建一个给定类名、方法名(通过正则表达式)和给定的返回值的方法,它看起来如下(清理到相关部分对于这个问题):

To do so, our Mock-Factory is providing a method to create a mock given the class-name, the method-name (by regexp) and the given return value which looks about the following (cleand up to the relevant parts for this question):

public <T> T getMockForMethod(Class<T> clazz, String methodName, Object methodResponse)
{
  T mockForMethod = mock(clazz);
  for (Method m : clazz.getDeclaredMethods ())
  {
    if (m.getName ().matches (methodName) && 
        m.getReturnType ().isAssignableFrom (methodResponse.getClass ()))
    {
      try
      {
         Class<?>[] paramTypes = m.getParameterTypes ();
         Object[] params = new Object[paramTypes.length];
         for (Object o : params)
         {
           o = Mockito.anyObject ();
         }
         Mockito.when (m.invoke (mockForService, params)).thenReturn (methodResponse);
      }
      catch (IllegalArgumentException e)
      {
        e.printStackTrace (System.err);
      }
      catch (IllegalAccessException e)
      {
        e.printStackTrace (System.err);
      }
      catch (InvocationTargetException e)
      {
        e.printStackTrace (System.err);
      }
    }
  }
  return mockForMethod;
}

如您所见,方法名称与名称(正则表达式)和正确的给定返回类型匹配.

As u can see the method name is matched by name (regexp) and the correct given return type.

它工作正常,但我对我必须构建人工参数数组params这一事实感到有些困扰!不,方法

It works fine, but i'm a bit bothered about the fact that i have to build the artificial parameter-array params! And no, the approach

Mockito.when (m.invoke (mockForService, Mockito.anyVararg ())).thenReturn(methodResponse);

没用!但我真的不明白为什么!?

didn't work! But i don't really understand why!?

谁能给我理由或以上代码的更好替代方法?

Can anyone give me the reason or a better alternative to the code above?

推荐答案

你不应该这样做.Mockito 是一个设计精良、易于学习、文档齐全且几乎是事实上的标准框架.而且它是类型安全的,不需要反射,这使得测试易于阅读和理解.

You shouldn't do this. Mockito is a really well-designed, simple to learn, extremely well-documented, and almost de-facto standard framework. And it's type-safe and doesn't need reflection, which makes tests easy to read and understand.

让您的开发者学习真正的 Mockito 并直接使用其 API.他们会很乐意使用它,因为它比您自己的超级 api 具有更好、更易于使用和更灵活的设计,并且他们会知道他们不会无缘无故地学习 Mockito,因为他们可能会使用它在其他项目甚至其他工作中.

Let your developers learn the real Mockito and use its API directly. They'll be happy to use it because it will have a better, easier to use and more flexible design than your own super-api, and they'll know that they won't learn Mockito for nothing because they'll probably use it in other projects or even other jobs.

Mockito 不需要其他专有 API.因此,我建议的替代方案是忘记这一点,并将 Mockito 教给您的开发人员.

Mockito doesn't need another proprietary API on top of it. My suggested alternative is thus to forget about this and teach Mockito to your developers.

这篇关于使用 Mockito 通过反射模拟方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆