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

      <bdo id='7DVbp'></bdo><ul id='7DVbp'></ul>

      <tfoot id='7DVbp'></tfoot>

      1. <legend id='7DVbp'><style id='7DVbp'><dir id='7DVbp'><q id='7DVbp'></q></dir></style></legend>

        <small id='7DVbp'></small><noframes id='7DVbp'>

        尝试存根包保护方法时,Mockito 模拟调用真实方法

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

              <bdo id='rLUh9'></bdo><ul id='rLUh9'></ul>
                <tbody id='rLUh9'></tbody>

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

              <legend id='rLUh9'><style id='rLUh9'><dir id='rLUh9'><q id='rLUh9'></q></dir></style></legend>

                • <tfoot id='rLUh9'></tfoot>

                  本文介绍了尝试存根包保护方法时,Mockito 模拟调用真实方法实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在尝试使用 Mockito 1.8.5 对方法进行存根,但这样做会调用引发异常的真实方法实现(使用 "" 作为参数值).

                  I'm trying to stub a method using Mockito 1.8.5, but doing so calls the real method implementation (with "" as parm values) which throws an exception.

                  package background.internal; //located in trunk/tests/java/background/internal
                  
                  public class MoveStepTest {
                  
                      @Test
                      public void testMoveUpdate() {
                          final String returnValue = "value";
                          final FileAttachmentContainer file = mock(FileAttachmentContainer.class);
                          doReturn(returnValue).when(file).moveAttachment(anyString(), anyString(), anyString());
                          //this also fails
                          //when(file.moveAttachment(anyString(), anyString(), anyString())).thenReturn(returnValue);
                  
                          final AttachmentMoveStep move = new AttachmentMoveStep(file);
                          final Action moveResult = move.advance(1, mock(Context.class));
                          assertEquals(Action.done, moveResult);
                      }
                  }
                  

                  我试图模拟的方法如下所示.没有最终方法或类.

                  The method I'm trying to mock looks like this. There are no final method or classes.

                  package background.internal; //located in trunk/src/background/internal
                  
                  
                     public class FileAttachmentContainer {
                          String moveAttachment(final String arg1, final String arg2, final String arg3) 
                                  throws CustomException {
                              ...
                          }
                  
                          String getPersistedValue(final Context context) {
                             ...     
                          }
                      }
                  

                  我通过模拟的课程看起来像这样:

                  And the class I'm passing the mock looks like this:

                  package background.internal; //located in trunk/src/background/internal
                  public class AttachmentMoveStep {
                  
                      private final FileAttachmentContainer file;
                  
                      public AttachmentMoveStep(final FileAttachmentContainer file) {
                          this.file = file;        
                      }
                  
                      public Action advance(final double acceleration, final Context context) {
                          try {
                              final String attachmentValue = this.file.getPersistedValue(context);
                              final String entryId = this.file.moveAttachment(attachmentValue, "attachment", context.getUserName());
                  
                              //do some other stuff with entryId
                          } catch (CustomException e) {
                              e.log(context);
                          }    
                          return Action.done;
                      }
                  }
                  

                  是什么导致真正的实现被调用,我该如何防止它?

                  What is causing the real implementation to be invoked and how can I prevent it?

                  推荐答案

                  Mockito 代码无法访问您正在模拟的方法.

                  The method you are mocking is not accessible to the Mockito code.

                  因为您的测试代码和被测代码在同一个包中,编译器允许您以这种方式设置模拟,但在运行时,Mockito 库必须尝试访问 moveAttachment,但它不适用于您的情况.这似乎是 错误 或 已知限制,因为它应该支持这种情况,(事实上,在大多数情况下确实支持它).

                  Because your test code and your code under test are in the same package, the compiler lets you set up your mock that way, but at runtime, the Mockito library has to try to access moveAttachment, but it's not working in your case. This appears to be a bug or known limitation in Mockito as it should support that case, (and in fact, does support it in most cases).

                  最简单的方法是使 moveAttachment 成为公共方法.如果这不是一个选项,那么首先要问你是否想模拟它.如果调用真正的方法会发生什么?

                  The easiest thing to do would be to make moveAttachment a public method. If that is not an option, then first question whether you want to even mock it. What happens if the real method gets called?

                  最后一个选项是使用 PowerMock 来处理 moveAttachment 方法作为私有方法并以这种方式模拟它.

                  The last option is to use PowerMock to treat the moveAttachment method as a private method and mock it that way.

                  这篇关于尝试存根包保护方法时,Mockito 模拟调用真实方法实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:“Char 不能被取消引用"错误 下一篇:Mockito 模拟所有方法调用并返回

                  相关文章

                  最新文章

                    <bdo id='SLGRq'></bdo><ul id='SLGRq'></ul>
                • <tfoot id='SLGRq'></tfoot>

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

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

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