使用 Mockito 通过反射来模拟方法

Using Mockito to mock methods by reflection(使用 Mockito 通过反射来模拟方法)
本文介绍了使用 Mockito 通过反射来模拟方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我们正在使用 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 通过反射来模拟方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Sending a keyboard event from java to any application (on-screen-keyboard)(将键盘事件从 java 发送到任何应用程序(屏幕键盘))
How to make JComboBox selected item not changed when scrolling through its popuplist using keyboard(使用键盘滚动其弹出列表时如何使 JComboBox 所选项目不更改)
Capturing keystrokes without focus(在没有焦点的情况下捕获击键)
How can I position a layout right above the android on-screen keyboard?(如何将布局放置在 android 屏幕键盘的正上方?)
How to check for key being held down on startup in Java(如何检查在Java中启动时按住的键)
Android - Get keyboard key press(Android - 获取键盘按键)