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

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

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

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

      1. Mockito spy - 当调用内部类方法而不是间谍对象中的

        时间:2023-09-25
              <tbody id='9PFDc'></tbody>

              <legend id='9PFDc'><style id='9PFDc'><dir id='9PFDc'><q id='9PFDc'></q></dir></style></legend>

              <small id='9PFDc'></small><noframes id='9PFDc'>

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

                  本文介绍了Mockito spy - 当调用内部类方法而不是间谍对象中的间谍方法时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有如下内部类的类:

                  public class ClassWithInnerObject {
                  
                    private final InnerObject innerObject;
                  
                    public ClassWithInnerObject() {
                      innerObject = new InnerObject();
                    }
                  
                    public void callInnerObjectMethod() {
                      innerObject.outerFunc();
                    }
                  
                    public void outerFunc() {
                      innerFunc();
                    }
                  
                    public void innerFunc() {
                      Log.d("XXX", "innerFunc: called");
                    }
                  
                    public class InnerObject {
                      public void outerFunc() {
                        innerFunc();
                      }
                    }
                  }
                  

                  mockito 测试如下所示:build.gradle:

                  And the mockito test is looking as following: build.gradle:

                    androidTestCompile 'junit:junit:4.12'
                    androidTestCompile 'org.mockito:mockito-core:1.10.19'
                  
                    androidTestCompile 'com.crittercism.dexmaker:dexmaker:1.4'
                    androidTestCompile 'com.crittercism.dexmaker:dexmaker-mockito:1.4'
                    androidTestCompile 'com.crittercism.dexmaker:dexmaker-dx:1.4'
                  

                  测试:

                  @RunWith(AndroidJUnit4.class) public class SpyVerifyTest {
                  
                    @Test public void myInnerTestWorking() {
                      ClassWithInnerObject p = new ClassWithInnerObject();
                      ClassWithInnerObject spy = Mockito.spy(p);
                      spy.outerFunc();
                      verify(spy, times(1)).innerFunc();
                    }
                  
                  
                    @Test public void myInnerTestNotWorking() {
                      ClassWithInnerObject p = new ClassWithInnerObject();
                      ClassWithInnerObject spy = Mockito.spy(p);
                      spy.callInnerObjectMethod();
                      verify(spy, times(1)).innerFunc();
                    }
                  
                  }
                  

                  第一个测试按预期工作.第二个 innerFunc 从未被检测为已调用",尽管在日志中我看到它是.出了什么问题?:)

                  The first test is working as expected. The second one the innerFunc is never detected as "invoked", although in the log I see it is.What is wrong? :)

                  谢谢!

                  推荐答案

                  怎么了?

                  好吧,这里的问题很微妙,当你调用 Mockito.spy(p) 时,mockito 在你的 ClassWithInnerObject 允许监视实例上的所有方法调用.多亏了这一点,您可以检查给定方法被调用了多少次但仅在装饰器上而不是在您的实例上.在这里,当您使用内部类时,它会在您的 ClassWithInnerObject 实例上调用 innerFunc() 而不是在装饰器上调用 Mockito innerFunc() 没有被调用.

                  Well, the problem here is quite subtle, when you call Mockito.spy(p), mockito creates behind the scene some kind of decorator over your instance of ClassWithInnerObject allowing to monitor all methods calls on your instance. Thanks to that, you can check how many times a given method has been called but on the decorator only not on your instance. And here, when you use an inner class, it calls innerFunc() on your instance of ClassWithInnerObject not on the decorator so for Mockito innerFunc() has not been called.

                  这篇关于Mockito spy - 当调用内部类方法而不是间谍对象中的间谍方法时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何使用 PowerMock &amp; 模拟枚举类的实例莫基 下一篇:AspectJ 的集成测试

                  相关文章

                  最新文章

                • <small id='dv15Z'></small><noframes id='dv15Z'>

                    1. <legend id='dv15Z'><style id='dv15Z'><dir id='dv15Z'><q id='dv15Z'></q></dir></style></legend>
                        <bdo id='dv15Z'></bdo><ul id='dv15Z'></ul>

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