<legend id='c0fHA'><style id='c0fHA'><dir id='c0fHA'><q id='c0fHA'></q></dir></style></legend>
    • <bdo id='c0fHA'></bdo><ul id='c0fHA'></ul>

      <i id='c0fHA'><tr id='c0fHA'><dt id='c0fHA'><q id='c0fHA'><span id='c0fHA'><b id='c0fHA'><form id='c0fHA'><ins id='c0fHA'></ins><ul id='c0fHA'></ul><sub id='c0fHA'></sub></form><legend id='c0fHA'></legend><bdo id='c0fHA'><pre id='c0fHA'><center id='c0fHA'></center></pre></bdo></b><th id='c0fHA'></th></span></q></dt></tr></i><div id='c0fHA'><tfoot id='c0fHA'></tfoot><dl id='c0fHA'><fieldset id='c0fHA'></fieldset></dl></div>

      <tfoot id='c0fHA'></tfoot>
      1. <small id='c0fHA'></small><noframes id='c0fHA'>

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

        时间:2023-09-25
      2. <i id='29sPM'><tr id='29sPM'><dt id='29sPM'><q id='29sPM'><span id='29sPM'><b id='29sPM'><form id='29sPM'><ins id='29sPM'></ins><ul id='29sPM'></ul><sub id='29sPM'></sub></form><legend id='29sPM'></legend><bdo id='29sPM'><pre id='29sPM'><center id='29sPM'></center></pre></bdo></b><th id='29sPM'></th></span></q></dt></tr></i><div id='29sPM'><tfoot id='29sPM'></tfoot><dl id='29sPM'><fieldset id='29sPM'></fieldset></dl></div>
          <tbody id='29sPM'></tbody>

            <small id='29sPM'></small><noframes id='29sPM'>

            <tfoot id='29sPM'></tfoot>
            <legend id='29sPM'><style id='29sPM'><dir id='29sPM'><q id='29sPM'></q></dir></style></legend>
                • <bdo id='29sPM'></bdo><ul id='29sPM'></ul>
                • 本文介绍了使用 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 通过反射来模拟方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:ContentValues 的方法未被模拟 下一篇:无法监视 HttpSession/Mockito

                  相关文章

                  最新文章

                    <bdo id='KOgfV'></bdo><ul id='KOgfV'></ul>

                  <small id='KOgfV'></small><noframes id='KOgfV'>

                    <tfoot id='KOgfV'></tfoot>
                  1. <legend id='KOgfV'><style id='KOgfV'><dir id='KOgfV'><q id='KOgfV'></q></dir></style></legend>

                    1. <i id='KOgfV'><tr id='KOgfV'><dt id='KOgfV'><q id='KOgfV'><span id='KOgfV'><b id='KOgfV'><form id='KOgfV'><ins id='KOgfV'></ins><ul id='KOgfV'></ul><sub id='KOgfV'></sub></form><legend id='KOgfV'></legend><bdo id='KOgfV'><pre id='KOgfV'><center id='KOgfV'></center></pre></bdo></b><th id='KOgfV'></th></span></q></dt></tr></i><div id='KOgfV'><tfoot id='KOgfV'></tfoot><dl id='KOgfV'><fieldset id='KOgfV'></fieldset></dl></div>