Why Java Experts Avoid Mocks

What’re the correct mocking ways — fake, mock, or stub?

Milos Zivkovic
Javarevisited

--

Photo by RF._.studio from Pexels

“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.

--

--