I have an error condition that I want to test. The behavior I want to verify is that an error message gets written to the log. Since Mockito can’t stub static methods, this is rather difficult, because I want my class-under-test to either write directly to System.err.println() or my static Log.error() method. I don’t want to have to inject a mocked “logger object” into every single object that might write error messages!
So I guess what I’m asking is, what do you think is the best way to structure my Log class and/or the class-under-test so that I can stub out the logging methods or replace them with a mocked logger?
The best answer might not even make use of Mockito, it could be anything. I’d rather not import yet another library like PowerMock, but if you have a good answer that requires something like that I wouldn’t mind seeing it.
If you want to keep your logging logic in static methods, you can still initialize real logging implementation when the class is loaded based on some system property:
Then when you run your test you can specify a mock class name using -D property. But you will need to implement your mock logging class without mockito magic.