From: <lau...@ma...>
> On 8 May, Steve Freeman wrote:
> > Do you have an example?
>
> Well, something like this:
>
> Foo methodToTest() {
> ObjectHome home = mockFactory.getHomeObject();
> Object o = home.create()
>
> return o.getFoo();
> }
>
> In order for this method to be tested correctly, o has to be avalid
object.
> I have two way of doing this: either Home.create() implements all the
logic
> to create a correct Foo object (and in our case, sometimes that's a
> multi-step operation) or I add a setupFoo(Foo o) method that will
> effectively do the same thing, but the multi-step operation occurs
outside
> of the mock object that is returned by getHomeObject().
In that case, then you would probably want a mock factory, mock home
object, and mock o. It's a pain, but it's quite easy to set up using
anonymous implementations in the test class until the mock gets useful
enough to pull out into its own class: e.g.
public void testMethodToTest() {
final Foo aFoo = new Foo();
final AnObject mockO = new AnObject() {
public Foo getFoo() {
return aFoo;
}
};
final ObjectHome mockHome = new ObjectHome() {
public AnObject create() {
return mockO;
}
};
Factory mockFactory = new Factory() {
public HomeObject getHomeObject() {
return mockHome;
}
};
testObject.setFactory(mockFactory);
assertEquals("Should be a foo", aFoo, testObject.methodToTest());
}
or some such madness. It shouldn't be long before you can refactor out
all this setup into something more readable.
> I think what I need to understand is where do I stop reimplementing
the
> logic of my real application in the mock object?
almost immediately. If you reimplement the real application, then you've
just got more to test.
S.
|