java - Effects of Mockito Mocks on Unit Tests When Refactoring -
if use mockito mock of object being injected sut argument, happens if during refactoring code re-organized call non-mocked method of same mock? tests fail , i'd have go , change tests , set them new call (the opposite of i'd want doing when refactoring code)
if common occurrence during refactoring, how can using mocks of use except when mocking external, resource-intensive entities (network, db, etc.)?
i'm using mocks mock out objects take hours set given team seems love monstrously deep aggregate objects.
thanks!
you correct refactoring break code depends on mocks. mockito doesn't know method foo(int start, int end)
, foo(int start)
accomplish same thing, , if switch between them while refactoring, mockito mocks break. mockito provide reasonable defaults unstubbed calls, 0, null, or empty list; however, many refactors need more-realistic values.
in general, i've heard tendency of test or test fixture fail when system behaving correctly called "brittleness".
part of derived choice of mocking framework: mockito started life fork of easymock, easymock fail default if there either many or few calls, mockito ignore unexpected calls , otherwise provide "nice" default behavior. other part determined how use framework, verifying unnecessary details (unimportant calls or parameters) may make mocks more brittle have be.
things mockito @ mocking:
- external, resource-intensive dependencies (or wrappers).
- interfaces. little can go wrong here.
- small api surfaces. if api surface has 1 or 2 methods, it's unlikely catch situation you're describing.
- collaborators don't exist yet, if have large api surfaces. temporary brittle tests can fixed later.
things mockito not mocking:
- very large api surfaces , dsls. imagine implementing builder pattern mockito! prefer using real object, or writing in-memory fake or other test double.
- stateful objects databases , model objects. fakes better choice here, if real objects won't do.
- concrete classes don't control. @ point, implementation details choices of
final
start sneak in. might reason create wrapper control. - anything existing , well-tested implementation. why go through trouble , lose test fidelity when real implementation there?
it's worth saying no test double safe; system may cache, combine, delay, modify, or otherwise adjust interactions collaborators, , of can break pretty test double write. art of writing flexible test rely on few implementation details possible, balancing risk of brittleness against requirement thoroughly test system , interactions outside world.
all of said, directly answer question "how can using mocks of use", see jb nizet's great analogy here: if you're trying create bomb detonator, want test it, cost of using real thing great. difficulty involved (and optimal choice of test double) vary based on whether bomb in question has hundred little triggers, or single boom()
method.
for more details test doubles (a category includes mocks, fakes, , dummy objects) , pros , cons, see martin fowler's article "mocks aren't stubs".
Comments
Post a Comment