Answer the question
In order to leave comments, you need to log in
How to mock a static object with Mockito?
There is some object which contains a static field.
In the tests, I annotated the static field with Mock , and also indicated the following:
when(staticObject.getAnotherMethod()).thenReturn("test");
Answer the question
In order to leave comments, you need to log in
Short answer: no way, Mockito only allows locking local fields/methods
Long answer: you need to edit the code under test to add the ability from the test to replace the default value of a static field with a locked one. Or convert a static field to a local field, which is the recommended approach when writing code under test.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question