From: Paul J. <pau...@me...> - 2002-06-28 05:02:50
|
The approach we have been taking is to extend a Factory with a = MockFactory have a static Factory.setInstance(Factory f) that allows us = to setup the MockFactory as the real factory. That is assuming that we use a singleton pattern to return the Factory = (or MockFactory) with the static Factory.instance() method. Is this the best approach or is there a better one? For example, the code would look like this... public class Factory { private Factory factory =3D null; public static instance() { if (factory =3D=3D null) { factory =3D new Factory(); } return factory; } public static setInstance(Factory pFactory) { factory =3D pFactory; } public Bean getBean() { return new Bean(); } } public class MockFactory extends Factory { public Bean getBean() { return new MockBean(); } } Paul Jackson Meridian Informatics Phone: (02) 8233 7564 Mobile: 0418 855 496 -----Original Message----- From: moc...@li... [mailto:moc...@li...]On Behalf Of Olaf Kock Sent: Friday, 28 June 2002 3:43 AM To: Moc...@li... Subject: Re: [Mockobjects-java-users] MockObjects and Factories best practices Hi John, John Sisk wrote: >=20 > Hi there, >=20 > What is the best practice for integrating mocks into a project?=20 The key point is to shift object creation from classes to be tested to somewhere outside. That doesn't necessarily meen a factory to be involved, but the class in question will be parameterized with their collaborators from somewhere outside (probably/sometimes in the constructor). This way production code constructs objects and parameterizes them with "real" collaborators (and may do so with the use of factories or builders). Testcode on the other hand directly creates objects under test and parameterizes them with MockObjects suiting its particular needs. Don't pollute factories with mockobjects. (That is - if you wanted to ask that and I understood correctly). Production code should be able to be delivered without any MockObject. Hope that helps? Olaf --=20 abstrakt gmbh, Behringstrasse 16b, 22765 Hamburg=20 Tel: +49-40-39804630, Fax: +49-40-39804639 http://www.abstrakt.de/ |