From the BileBlog:
The major flaw with dynamic mocks is that the return values are for all intents and purposes static. So for example, you can specify something like myMock.expectAndReturn(“getBlah”, “blah”); but cannot express something where the return value is not static. Why might you want to do that, you might wonder. Well, lets say you want to create a mock object for ServletContext, you’d want to be able to specify a Map that methods like getInitParameter and get/setAttribute work off of. Alas, this is impossible without much arm twisting and ugly hackery. You basically have to implement your own CallFactory which has all the extra magic in it. Maybe I’m missing something, but a less braindead solution to me would be to have the possibility of return values implementing some kind of interface, and in that special case, use a callback to determine what the actual return should be.
I have done exactly this, for the the purposes of erecting a fake servlet engine around some code that needed a more dynamic interaction with its environment than a straight mock object could provide. I’ve even been given permission to open source it, I just haven’t gotten around to it yet. I’ll see what I can do about getting it out there.