Hi Math,
I'd suggest the same as all the previous writers have:
The usage of dedicated Mock Frameworks kind of misses the point!
I think that a unit test case should always inspect a specific part of
the overall behavior of the unit it is inspecting. In my experiences, a
lot of people confuse this with "a unit test case should always inspect
a specific method of the unit it is inspecting".
Using mocking frameworks moves the focus in testing to "inspecting
methods" whereas it should be "inspecting behavior".
If you on the other hand construct specific test doubles, you are likely
to express the actual intent of the test.
Consider the following examples:
```
UserRegistration user_registration = mock(UserRegistration.class)
when(user_registration.addAccount(user_profile)).thenThrow(new
InvalidUserProfile());
// inject user_registration into the unit ...
```
vs
```
UserRegistration user_registration = FailingUserRegistration.new
// inject user_registration into the unit ...
```
The first one screams IMPLEMENTATION all over the place and nearly hides
the fact that you want to test behavior in an invalid case.
The other implementation tries to hide all the particular details from
the test and instead provides a tailored (and probably reusable) double.
I hope I could communicate my main reason why I am not using mocking
frameworks anymore. I feel that my code bases have become less brittle
and more readable.
Cheers
Jakob
On 02/17/2015 02:16 PM, Math Newman wrote:
> This is an issue that's been plaguing me for quite a bit and I'm struggling
> to reach a definitive conclusion.
>
>
> The company I'm currently working for uses mocking frameworks in their
> tests and it brought about a pretty serious consequence, there is mocking
> framework "set up" code splattered all over the test classes.
>
> Not only does this make the tests incredibly hard to read, especially for
> people unfamiliar with the framework, but it seems to make the tests more
> fragile because since none of the mocking code is re-used, changes to the
> production code can break many tests for the same reason.
>
>
> The conclusion I've reached is that by using mocking frameworks instead of
> creating separate classes for their test doubles, they are violating the
> SRP principle because the test class is now responsible for the test
> double's logic *and* it has to perform the actual tests with them.
>
> Would I be correct in thinking that in almost all cases, test doubles
> should reside in separate classes and that our tests should only be
> responsible for using the ones they need in for their specific tests?
>
> Or is there something obvious that I'm missing?
>
--
Jakob Holderbaum, M.Sc.
Embedded Software Engineer
0176 637 297 71
http://jakob.io/
http://jakob.io/mentoring/
h...@jakob.io
@hldrbm