Closed GoogleCodeExporter closed 8 years ago
Hi,
I'll try to find some time to implement it.
Thanks for reporting!
Original comment by szcze...@gmail.com
on 14 Nov 2009 at 11:40
Thanks. I just came across the discussion list about "deep stubbing". Using
RETURNS_MOCK didn't work for
me, but this worked:
private final static Answer<Object> MY_RETURNS_MOCKS = new Answer<Object>() {
private static final long serialVersionUID = -6926328908792880098L;
private final HashMap<Class<?>, Object> mocks = new HashMap<Class<?>, Object>();
public Object answer(InvocationOnMock invocation) throws Throwable {
Class<?> clz = invocation.getMethod().getReturnType();
if (mocks.containsKey(clz)) {
return mocks.get(clz);
} else {
Object mock = mock(clz);
mocks.put(clz, mock);
return mock;
}
}
};
Needless to say, this requires a bit of testing and error checking (e.g.
handling final classes).
Original comment by mahm...@notnoop.com
on 14 Nov 2009 at 11:47
Very nice. Thanks for submitting!
Original comment by szcze...@gmail.com
on 15 Nov 2009 at 8:30
Needless to say my code is quite buggy. I'll try to clean it up.
Original comment by mahm...@notnoop.com
on 15 Nov 2009 at 8:44
I had a need for this in practice, so I adapted your code into this.
It seems to work fairly well but I haven't tested it _that_ much.
public class MockitoUtil {
public static <T> T deepMock(Class<T> clazz) {
return Mockito.mock(clazz, new DeepAnswer());
}
}
class DeepAnswer implements Answer<Object> {
private static final long serialVersionUID = -6926328908792880098L;
private final HashMap<Class<?>, Object> mocks = new HashMap<Class<?>, Object>();
public Object answer(InvocationOnMock invocation) throws Throwable {
Class<?> clz = invocation.getMethod().getReturnType();
if (clz.isPrimitive()) {
return null;
}
if (mocks.containsKey(clz)) {
return mocks.get(clz);
} else {
Object mock = Mockito.mock(clz, this);
mocks.put(clz, mock);
return mock;
}
}
}
Original comment by kristofer.karlsson@gmail.com
on 17 Nov 2009 at 4:41
Don't worry too much - we'll clean it up. Unfortunately, you might hit the
problem
revealed in issue 151 :( However, I'll fix 151 quickly and release it with 1.8.1
Original comment by szcze...@gmail.com
on 20 Nov 2009 at 9:28
Yes, I did run into issue 151, and got stuck; except that I thought it was my
fault! Attached is my set of
smoke tests. Hope you find them helpful.
When trying to handle invocations of different arguments, what's the proper way
of comparing invocations? I
am referring to the withArguments() and withPatternArguments() test use cases.
Noticed that
InvocationOnMock doesn't override .hashCode().
Also, I had a difficult time debugging with the Eclipse debugger. Mock
underlying representations and
mockingProgress and iOngoingStubbings are getting corrupted somehow (their
values change during the
exception), with some calls to .toString(). I assume that the debugger makes
toString() calls, while it is
stepping through when() argument, so .toString() also gets recorded as part of
the expectation as well.
Original comment by mahm...@notnoop.com
on 20 Nov 2009 at 1:11
Attachments:
>Yes, I did run into issue 151, and got stuck;
151 is very easy to fix. I even did it already but couldn't check-in due to
problems
with my hgsubversion setup :)
>When trying to handle invocations of different arguments, what's the proper
way of
>comparing invocations?
Ouch... That's not easy. I'll write some more later.
>Also, I had a difficult time debugging with the Eclipse debugger.
Yeah, I know what you mean but we have to live with it :] Try not to toString()
mocks
and it should not be such a hassle.
We can make Invocation implement hashCode() easily if needed.
Original comment by szcze...@gmail.com
on 20 Nov 2009 at 1:54
>comparing invocations?
It's not equals() because sometimes parameters are given via ArgumentMatchers.
You
know all the eq(), anyObject() stuff. Therefore here's how I compare invocations
internally:
invocationMatcher.matches(invocation)
We'll look at your implementation sometime soon ;)
Original comment by szcze...@gmail.com
on 3 Dec 2009 at 9:25
Attached is a patch that enables deep stubbing (sounded better than chained
expectations for this project). I
generate the mocks using not-so-kosher access to InvocationContainerImpl, as it
seems to be my only hope to
access ArgumentMatches not just the final invocation.
The patch also includes some tests. I only tested with patterns and such.
Didn't test it with consecutive
stubbing (don't expect it to work), or other syntaxes of stub generation (e.g.
doReturn(...).when(...)).
Original comment by notn...@gmail.com
on 5 Dec 2009 at 6:08
Attachments:
This issue was closed by revision r1816.
Original comment by szcze...@gmail.com
on 1 Jan 2010 at 4:37
In trunk.
Thanks for sharing! I'll do some refactorings later on.
Cheers!
Szczepan
Original comment by szcze...@gmail.com
on 1 Jan 2010 at 4:41
Original comment by szcze...@gmail.com
on 28 Feb 2010 at 9:03
This issue was closed by revision faa7575781.
Original comment by szcze...@gmail.com
on 7 Oct 2010 at 5:21
Original comment by szcze...@gmail.com
on 3 Jul 2011 at 12:43
Original issue reported on code.google.com by
mahm...@notnoop.com
on 14 Nov 2009 at 10:31