Rookian
Rookian

Reputation: 20549

Why should I favour stubs over mocks?

I often read that one should avoid mocks and prefer stubs.

The Isolation Frameworks like FakeItEasy and NSubstitute make it most of the time really easy to mock or stub a dependency. Those frameworks themselves don't distinguish between stubs or mocks, but call them altogether Fakes or Substitutes. As far as I know in the early days of "Mocking" it was very hard to create mock objects, because using the cumbersome Record and Replay mechanism. But today it seems like there is no big difference anymore.

So why should I favor stubs over mocks? Why are mocks more brittle than stubs?

Upvotes: 2

Views: 379

Answers (2)

Mark Seemann
Mark Seemann

Reputation: 233192

You shouldn't 'prefer' Stubs over Mocks; rather, you should select the right tool for the job:

Mocks for Commands, Stubs for Queries

Upvotes: 3

Rogério
Rogério

Reputation: 16380

At least with recent mocking tools, there is no point in bothering with stubs versus mocks. In practice, mock and stubs are the same thing when created by a mocking library. And this is how it should be.

With the mocking libraries available for Java (and for C#.NET too, if I am not mistaken), you can't create "just" a stub. You always get a mock object on which expectations can be verified.

Upvotes: -1

Related Questions