Mocking member variables of a class using Mockito

前端 未结 9 1799
醉话见心
醉话见心 2020-12-07 10:24

I am a newbie to development and to unit tests in particular . I guess my requirement is pretty simple, but I am keen to know others thoughts on this.

Suppose I h

9条回答
  •  既然无缘
    2020-12-07 10:46

    If you can't change the member variable, then the other way around this is to use powerMockit and call

    Second second = mock(Second.class)
    when(second.doSecond()).thenReturn("Stubbed Second");
    whenNew(Second.class).withAnyArguments.thenReturn(second);
    

    Now the problem is that ANY call to new Second will return the same mocked instance. But in your simple case this will work.

提交回复
热议问题