From: Olaf K. <ok...@ab...> - 2002-07-23 14:02:21
|
Oren Gross wrote: > > If so, how can use mock objects for testing > objects that do not expect such an argument, > but create an instance of it inside? Hi Oren, probably the nicest thing in TestDrivenDevelopment is, that the responsibility for creation of objects gets turned upside down - or inside out if you prefer so... If you write Tests that want to parameterize an object with a mock implementation you need a way to provide this from outside. The bad way (but sometimes reasonable) would be to "inject" a MockObject, using a method that is there only for injecting it during the test run. A probably better way would be to have the subsystem designed in a way that they get their collaborators set from outside, e.g. during creation in a factory or builder (or testcase). You get this almost naturally with TestDrivenDevelopment (http://c2.com/cgi/wiki?TestDrivenDevelopment). In any case, you need to refactor the creation of the objects to a place both the runtime- and the test-environment can access. One thing more: Beware of using MockObject to replace each and every instance of collaborator interfaces in testing: You might loose flexibility and the ability to refactor mercilessly. <shamelessAdvert> Beware of it even if there is a new version of MockCreator available for Eclipse, VAJ and CommandLine (www.abstrakt.de/mockcreator.html) ;-) </shamelessAdvert> But of course, your milage may vary... My advice is to get deep into TestDrivenDevelopment, this will almost automagically lead to MockObjects at some point (if this is not the way you went up to where you are now;-) Olaf -- abstrakt gmbh, Behringstrasse 16b, 22765 Hamburg Tel: +49-40-39804630, Fax: +49-40-39804639 http://www.abstrakt.de/ |