From: "Oren Gross" <og...@me...>
To: <moc...@li...>
Sent: Tuesday, July 23, 2002 12:00 PM
Subject: [Mockobjects-java-users] how to use?
> Hi there
> I am probably the most fresh mock-objects user ever (I can count me
> experience in minutes).
> As a starter, I have a feeling that the only way for me to use mock
objects,
> is by implementing a basic interface used by the tested object, and
sending
> it to this object, in order for it to use the mock and not the original
> object. If so, how can use mock objects for testing objects that do not
> expect such an argument, but create an instance of it inside?
> Thanks, Oren
one of the interesting effects of using mock objects is that it drives you
to pass objects around rather than hide them, which turns out to have some
nice effects on composability. In your case, depending on the circumstances,
you might pass in a factory. You can then subsitute another factory that
returns mocks -- it's a little bit of a pain, largely driven by inadequacies
in Java's object model. Alternatively, if the inner object has the same
lifetime as the container, we quite often cheat a little and try tricks like
overloading the contstructor:
public class Thing {
public Thing(InnerThing innerThing) {
inner = innerThings;
}
public Thing() {
this(new DefaultInnerThing());
}
}
public class ThingTest {
public void testOne() {
MockInnerThing mockInner = new MockInnerThing();
Thing aThing = new Thing(mockInner);
...
public class RealCode {
...
Thing aThing = new Thing();
// do something with it...
Steve
|