Why Java Experts Avoid Mocks
What’re the correct mocking ways — fake, mock, or stub?
Published in
5 min readSep 16, 2021
“Don’t use mocks.” ¹ “Mocks are stupid.” ²
Easier said than done. Mocks are everywhere. You can’t test without mocks.
What are the alternatives? Why mocks aren’t “one-size-fits-all”? How to use other test doubles?
What mock alternatives do you have?
Only mocks exist. Mocks are enough.
Let’s look at the following code example.
What’s wrong with the example? engine
. This shouldn’t be mocked. There’s no usage of engine
in the test. Mock adds more boilerplate and doesn’t serve the purpose.
Let’s look at another example.