Marty Andrews

artful code

Saturday, January 10, 2004

mock objects rock! Mock Objects suck!

Developers using Mock Objects aren't getting better designed code. They're following the trend of the community to use Mock Object projects without understanding the mock philosophy (sic). Using mock objects is a design technique, but Mock Objects only provide a testing technique. If people use mock objects instead of Mock Objects, their designs will improve.In case you haven't noticed yet, I think there's two things here that need to be considered separately - "Mock Objects" and "mock objects"."Mock Objects" (with capitals) are tools that try to automagically fake API's of 3rd party libraries which get used in Java programs. They are used as a *testing* technique. I think they are bad."mock objects" (without capitals) are just a way for developers to stub implementations of parts of their own code (not 3rd party libraries). Developers write the mocks themselves rather than having it generated. They are used as a *design* technique. I think they are good.The reason I like "mock objects" is that you use them in conjunction with TDD to drive the design of your code. The code getting mocked is at least one layer in from the 3rd party libraries, so you end up with better abstractions in your code and a much cleaner design.The reason I don't like "Mock Objects", is that you end up with complex mock behaviour (mainly because of complex 3rd party API's). It's also easy to get a bit lazy because your design is not being driven anymore.