Mocking inter-method dependencies
Posted
by Zecrates
on Stack Overflow
See other posts from Stack Overflow
or by Zecrates
Published on 2009-10-19T06:11:21Z
Indexed on
2010/03/20
11:11 UTC
Read the original article
Hit count: 402
I've recently started using mock objects in my tests, but I'm still very inexperienced with them and unsure of how to use them in some cases. At the moment I'm struggling with how to mock inter-method dependencies (calling method A has an effect on the results of method B), and whether it should even be mocked (in the sense of using a mocking framework) at all?
Take for example a Java Iterator? It is easy enough to mock the next() call to return the correct values, but how do I mock hasNext(), which depends on how many times next() has been called? Currently I'm using a List.Iterator as I could find no way to properly mock one.
Does Martin Fowler's distinction between mocks and stubs come into play here? Should I rather write my own IteratorMock?
Also consider the following example. The method to be tested calls mockObject.setX() and later on mockObject.getX(). Is there any way that I can create such a mock (without writing my own) which will allow the returned value of getX to depend on what was passed to setX?
© Stack Overflow or respective owner