1

As I understood, Mockito @Mock created a stub with empty methods or methods that otherwise return a default object for each type. On contrary to @Spy which wraps the instance so that you use the actual implementation but still be able to mock certain methods.

However, I have a code such as:

@Mock
private lateinit var context: Context
@Mock
private lateinit var mockConfig: ConfigUtil

And whenever I debug a test I have context being a Context@MockitoMock instance, and mockConfig a real ConfigUtil instance, therefore, calling any mockConfig method executes the real ConfigUtil implementation.

So, does Mockito only stub methods which are not on the build ? (such as Android classes).

--- Q2 ---

Is it a good practice to use @Mock in an object which is meant to test the real method implementation? or only on the dependencies that the instance uses.

In other words, If I create the ConfigUtil class, and the test suite is for that class, does it make any sense to create a @Mock instance of that class?

htafoya
  • 18,261
  • 11
  • 80
  • 104
  • 1
    Which version and what dependency of mockito are you using (pure mockito / mockito-kotlin)? I guess that `mockito`is unable to mock `ConfigUtil` and fails silently. In that case you might end up with a real instance. You might want to add more information regarding that class to your question. – second Nov 19 '19 at 18:45
  • 1
    Regarding Q2: You should not mock the class under test. Spying is (best used) for legacy code that you are not allowed to change. So, no - it does not make sense. – second Nov 19 '19 at 18:46
  • @second We are using `kotlin` in this case – htafoya Nov 19 '19 at 21:34
  • Hey @htafoya, did you get an answer to this query? I came across the same today, and observed the same behaviour from Mockito. It could create a good mock class only for the interface-based objects - not for the ones not implementing Intercaes(I suppose ConfigUtil is one such class in your case). – Raj May 26 '22 at 05:44
  • @Raj I don't remember, this was at 2019 when I was starting to learn Mockito. There seems to be no reason for that behaviour to happen, probably the runner was not applied or a missenterpretation of the debug output. – htafoya May 26 '22 at 20:35

0 Answers0