Mockito spy - when calling inner class method not spying method in spy object

后端 未结 1 1346
伪装坚强ぢ
伪装坚强ぢ 2021-01-13 04:11

I have class with inner class as following:

public class ClassWithInnerObject {

  private final InnerObject innerObject;

  public ClassWithInnerObject() {
         


        
相关标签:
1条回答
  • 2021-01-13 04:31

    What is wrong?

    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.

    0 讨论(0)
提交回复
热议问题