|
From: Nat P. <nat...@b1...> - 2002-12-18 02:15:27
|
The way round this problem is to extend the Mock class and implement special
behaviour for the methods that you want to be mocked differently --
findByAndCache in your example. If you define findByAndCache in the a class
derived from Mock with the same argument types as defined in the mocked
interface, the Mock class will call that implementation in preference to its
default mock implementation. You can invoke the default implementation by
calling the mockCall method if you want to within findByAndCache.
E.g.
class MockCachedFinder extends Mock {
public MockCachedFinder() {
super( CachedFinder.class );
}
List findByAndCache( String name, ... other args ... ) {
... implement special mock behaviour in here
}
}
In my experience, however, I have often found that having to put logic into
a mock object is an indication that there is something wrong with my design.
Perhaps you are testing too much in one go, and each interaction should
instead be tested in an individual test case. Perhaps your method does too
much, and should instead be split into methods with different names that can
each be mocked individually. I can't suggest more than that from the little
I have seen, but I have often found that following the "Tao of the Mock
Object" leads me to cleaner designs.
Cheers,
Nat.
________________________________
Dr. Nathaniel Pryce
B13media Ltd.
Studio 3a, Aberdeen Business Centre, 22/24 Highbury Grove, London, N5 2EA
http://www.b13media.com
----- Original Message -----
From: "Patrick Lightbody" <pli...@ci...>
To: <moc...@li...>
Sent: Tuesday, December 17, 2002 7:05 PM
Subject: [MO-java-dev] Using same method name, different args
> I really like the dynamic mock stuff, but it seems there is a pretty major
> flaw: MockExceptedCalls are mapped using _only_ the methodName. That means
I
> can't expect multiple values for different args on the same method:
>
> m.expectAndReturn("findByAndCache",
> C.args(C.eq(DocumentProblemType.ENTITY_NAME),
> C.IS_ANYTHING),
> UtilMisc.toList(dpt1, dpt2));
> m.expectAndReturn("findByAndCache",
> C.args(C.eq(DocumentSubject.ENTITY_NAME), C.IS_ANYTHING),
> Collections.EMPTY_LIST);
> m.expectAndReturn("findByAndCache",
> C.args(C.eq(Publishaction.ENTITY_NAME), C.IS_ANYTHING),
> Collections.EMPTY_LIST);
> m.expectAndReturn("findByAndCache",
> C.args(C.eq(Reviewaction.ENTITY_NAME), C.IS_ANYTHING),
> Collections.EMPTY_LIST);
>
> This code doesn't work becuase the most recent expectation overrides the
> previous one, and my code ends up failing. Am I using the Mock object
> incorrectly, or is there a way around this?
>
> -Pat
>
>
>
> -------------------------------------------------------
> This sf.net email is sponsored by:
> With Great Power, Comes Great Responsibility
> Learn to use your power at OSDN's High Performance Computing Channel
> http://hpc.devchannel.org/
> _______________________________________________
> Mockobjects-java-dev mailing list
> Moc...@li...
> https://lists.sourceforge.net/lists/listinfo/mockobjects-java-dev
|