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

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

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

      1. Mockito - 拦截模拟上的任何方法调用

        时间:2023-09-25
            <tbody id='eVVS1'></tbody>
            <bdo id='eVVS1'></bdo><ul id='eVVS1'></ul>

            • <legend id='eVVS1'><style id='eVVS1'><dir id='eVVS1'><q id='eVVS1'></q></dir></style></legend>

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

                  <tfoot id='eVVS1'></tfoot>
                • <small id='eVVS1'></small><noframes id='eVVS1'>

                  本文介绍了Mockito - 拦截模拟上的任何方法调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  是否可以以通用方式拦截 mock 上的所有方法调用?

                  Is it possible to intercept all method invocations on a mock in a generic way?

                  示例

                  给定一个供应商提供的类,例如:

                  Given a vendor provided class such as:

                  public class VendorObject {
                  
                      public int someIntMethod() {
                          // ...
                      }
                  
                      public String someStringMethod() {
                          // ...
                      }
                  
                  }
                  

                  我想创建一个模拟,将 all 方法调用重定向到另一个具有匹配方法签名的类:

                  I would like to create a mock that re-directs all method calls to another class where there are matching method signatures:

                  public class RedirectedToObject {
                  
                      public int someIntMethod() {
                          // Accepts re-direct
                      }
                  
                  }
                  

                  Mockito 中的 when().thenAnswer() 构造似乎符合要求,但我找不到将任何方法调用与任何参数匹配的方法.无论如何,InvocationOnMock 肯定给了我所有这些细节.有没有通用的方法来做到这一点?看起来像这样的东西,其中 when(vo.*) 被替换为适当的代码:

                  The when().thenAnswer() construct in Mockito seems to fit the bill but I cannot find a way to match any method call with any args. The InvocationOnMock certainly gives me all these details anyway. Is there a generic way to do this? Something that would look like this, where the when(vo.*) is replaced with appropriate code:

                  VendorObject vo = mock(VendorObject.class);
                  when(vo.anyMethod(anyArgs)).thenAnswer(
                      new Answer() {
                          @Override
                          public Object answer(InvocationOnMock invocation) {
                  
                              // 1. Check if method exists on RedirectToObject.
                              // 2a. If it does, call the method with the args and return the result.
                              // 2b. If it does not, throw an exception to fail the unit test.
                  
                          }
                      }
                  );
                  

                  在供应商类周围添加包装器以简化模拟不是一种选择,因为:

                  Adding wrappers around the vendor classes to make mocking easy is not an option because:

                  1. 现有代码库太大.
                  2. 对性能要求极高的应用程序的一部分.

                  推荐答案

                  我想你想要的是:

                  VendorObject vo = mock(VendorObject.class, new Answer() {
                      @Override
                      public Object answer(InvocationOnMock invocation) {
                  
                          // 1. Check if method exists on RedirectToObject.
                          // 2a. If it does, call the method with the args and return the
                          // result.
                          // 2b. If it does not, throw an exception to fail the unit test.
                  
                      }
                  });
                  

                  当然,如果你想经常使用这种方法,Answer 不需要匿名.

                  Of course, if you want to use this approach frequently, no need for the Answer to be anonymous.

                  来自 文档:这是一个非常高级的功能,通常你不需要它来编写体面的测试.但是在使用遗留系统时它会很有帮助."听起来像你.

                  From the documentation: "It's quite advanced feature and typically you don't need it to write decent tests. However it can be helpful when working with legacy systems." Sounds like you.

                  这篇关于Mockito - 拦截模拟上的任何方法调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:此处检测到错位的参数匹配器.您不能在 Mockito 中 下一篇:在同一个模拟上使用多个 ArgumentMatchers

                  相关文章

                  最新文章

                • <legend id='fxLg5'><style id='fxLg5'><dir id='fxLg5'><q id='fxLg5'></q></dir></style></legend>

                    • <bdo id='fxLg5'></bdo><ul id='fxLg5'></ul>

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

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

                      <tfoot id='fxLg5'></tfoot>