Proposal: Public access to the invocations of a mock.

8 views
Skip to first unread message

Martin Heidegger

unread,
Jul 25, 2010, 8:15:03 AM7/25/10
to mockito-flex
I tried to cover that all calls done to a mock are done in order and
that I didn't miss any. inOrder is useful to check whether the
invocations are done in that order but its not possible to find out if
no other was called in the end. If I had access to the invocations it
was possible to write a util that checks whether or not all methods
are called. I guess this would also find other uses.

Anyway: attached you find the diff for that proposal, what do you
think?


diff --git a/mockito/src/main/flex/org/mockito/Mockito.as b/mockito/
src/main/flex/org/mockito/Mockito.as
--- a/mockito/src/main/flex/org/mockito/Mockito.as
+++ b/mockito/src/main/flex/org/mockito/Mockito.as
@@ -21,6 +21,7 @@
package org.mockito
{
import org.mockito.api.Answer;
+import org.mockito.api.Invocations;
import org.mockito.api.Matcher;
import org.mockito.api.MethodSelector;
import org.mockito.api.MockCreator;
@@ -244,6 +245,11 @@
return mockCreator.mock(clazz, name, args);
}

+ public function getInvocationsFor(mock:*):Invocations
+ {
+ return mockInterceptor.getInvocationsFor(mock);
+ }
+
/**
* A starter function for verification of executions
* If you dont specify the verifier, an equivalent of
times(1) is used.
diff --git a/mockito/src/main/flex/org/mockito/api/MockInterceptor.as
b/mockito/src/main/flex/org/mockito/api/MockInterceptor.as
--- a/mockito/src/main/flex/org/mockito/api/MockInterceptor.as
+++ b/mockito/src/main/flex/org/mockito/api/MockInterceptor.as
@@ -52,5 +52,7 @@
* @see org.mockito.api.Matcher
*/
function addMatcher(matcher:Matcher):void
+
+ function getInvocationsFor(mock:*):Invocations;
}
}
\ No newline at end of file
diff --git a/mockito/src/main/flex/org/mockito/impl/
MockInterceptorImpl.as b/mockito/src/main/flex/org/mockito/impl/
MockInterceptorImpl.as
--- a/mockito/src/main/flex/org/mockito/impl/MockInterceptorImpl.as
+++ b/mockito/src/main/flex/org/mockito/impl/MockInterceptorImpl.as
@@ -32,6 +32,10 @@
import org.mockito.api.SequenceNumberTracker;
import org.mockito.api.Verifier;

+ import mx.collections.ArrayCollection;
+
+ import flash.utils.Dictionary;
+
public class MockInterceptorImpl implements MockInterceptor
{
public var invocationsMap:Dictionary = new Dictionary();
@@ -57,7 +61,7 @@
{
try
{
- var invocations:Invocations =
getInvocationFor(invocation.target);
+ var invocations:Invocations =
getInvocationsFor(invocation.target);
verifier.verify(invocation, invocations);
return null;
}
@@ -73,8 +77,8 @@
rememberInvocation(invocation);
}

- return
getInvocationFor(invocation.target).answerFor(invocation);
+ return
getInvocationsFor(invocation.target).answerFor(invocation);
}

public function
rememberInvocation(invocation:Invocation):void
{
@@ -77,8 +81,8 @@
}

public function
rememberInvocation(invocation:Invocation):void
{
- var invocations:Invocations =
getInvocationFor(invocation.target);
+ var invocations:Invocations =
getInvocationsFor(invocation.target);
invocations.addInvocation(invocation);
latestInvocation = invocation;
}
@@ -90,7 +94,7 @@
latestInvocation.addAnswer(answer);
}

- protected function
getInvocationFor(target:Object):Invocations
+ public function getInvocationsFor(target:*):Invocations
{
var invocations:Invocations = invocationsMap[target];
if (!invocations)
@@ -103,7 +107,7 @@

public function getInvocations(mock:Object):ArrayCollection
{
- return
getInvocationFor(mock).getEncounteredInvocations();
+ return
getInvocationsFor(mock).getEncounteredInvocations();
}

public function set verifier(value:Verifier):void

Kris

unread,
Jul 27, 2010, 12:24:37 PM7/27/10
to mockit...@googlegroups.com
Hi Martin,

So do you want to check if there are no other methods called on your mock in addition to what you expect? I guess I could a helper function to retrieve all the invocations however I'm not comfortable with adding an explicit function to the Mockito class. I would rather have an accessor to the MockInterceptor instance and extend it with a public function to retrieve invocations. I simply don't want to confuse users with functions exposing the internal details.

Would it work for you?

Regards,
Kris


--
You received this message because you are subscribed to the Google Groups "mockito-flex" group.
To post to this group, send email to mockit...@googlegroups.com.
To unsubscribe from this group, send email to mockito-flex...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mockito-flex?hl=en.


Martin Heidegger

unread,
Jul 27, 2010, 5:59:28 PM7/27/10
to mockito-flex
Hi Kris,

yes, you got me right :)

The MockInterceptor is not available directly to the public, which
makes it impossible to write universal utils. It would be possible to
implement a special solution, but pretty unhandy and with workaround.
I am not sure why the invocations should be hidden away?! A verifier
receives the invocations too, easy to implement.

sincerly yours
Martin.

On Jul 27, 6:24 pm, Kris <kris.karczmarc...@gmail.com> wrote:
> Hi Martin,
>
> So do you want to check if there are no other methods called on your mock in
> addition to what you expect? I guess I could a helper function to retrieve
> all the invocations however I'm not comfortable with adding an explicit
> function to the Mockito class. I would rather have an accessor to the
> MockInterceptor instance and extend it with a public function to retrieve
> invocations. I simply don't want to confuse users with functions exposing
> the internal details.
>
> Would it work for you?
>
> Regards,
> Kris
>
> > mockito-flex...@googlegroups.com<mockito-flex%2Bunsu...@googlegroups.com>
> > .

Kris

unread,
Jul 28, 2010, 1:02:59 AM7/28/10
to mockit...@googlegroups.com
Martin,

My concern is to add more and more public functions to the Mockito class that are unrelated to the typical use. Expert users like you will have no problem dealing with MockInterceptor which is part of the API. The only difference for your util is that it's going to reach deeper for the invocations: mockito.interceptor.getInvocationsFor(mock)

The missing part is that the MockInterceptor needs a public getInvocationsFor() and I need to add getter for the inteceptor.

Another thing to concern is to add verifyNoMoreInteractions() functionality to Mockito. Java version has.

Hope this works for you.

Cheers,
Kris

To unsubscribe from this group, send email to mockito-flex...@googlegroups.com.

Martin Heidegger

unread,
Jul 28, 2010, 5:38:46 PM7/28/10
to mockito-flex
A verifyNoMoreInteractions() method would be really great. (since its
basically what I implemented)
To concider: verifyNoMoreInteractions() might be problematic, having
two mocks where only one should not have more interactions, it might
be good to have a optional "mock" attribute that limits it.

verifyNoMoreInteractions( allMocks ); // or so...

The Mockinterceptor is a pretty big hazzle to setup, and any tool i'd
make would confuse my colleagues, not the preferred solution for me.
But I can speak just on behalf of myself, you should do what you think
its best for your project.

yours
Martin.


On Jul 28, 7:02 am, Kris <kris.karczmarc...@gmail.com> wrote:
> Martin,
>
> My concern is to add more and more public functions to the Mockito class
> that are unrelated to the typical use. Expert users like you will have no
> problem dealing with MockInterceptor which is part of the API. The only
> difference for your util is that it's going to reach deeper for the
> invocations: mockito.interceptor.getInvocationsFor(mock)
>
> The missing part is that the MockInterceptor needs a public
> getInvocationsFor() and I need to add getter for the inteceptor.
>
> Another thing to concern is to add verifyNoMoreInteractions() functionality
> to Mockito. Java version has.
>
> Hope this works for you.
>
> Cheers,
> Kris
>
> > <mockito-flex%2Bunsu...@googlegroups.com<mockito-flex%252Buns...@googlegroups.com>

Kris

unread,
Jul 28, 2010, 5:49:39 PM7/28/10
to mockit...@googlegroups.com
Yeah the optional parameter if not given would mean all mocks.

On the MockInterceptor - no need to set up anything MockInterceptor is already there for you but there is no way to access it.

Regards,
Kris

To unsubscribe from this group, send email to mockito-flex...@googlegroups.com.

Martin Heidegger

unread,
Aug 6, 2010, 2:29:26 PM8/6/10
to mockito-flex
Well, can't wait for the implementation.

yours
Martin.

On Jul 28, 11:49 pm, Kris <kris.karczmarc...@gmail.com> wrote:
> Yeah the optional parameter if not given would mean all mocks.
>
> On the MockInterceptor - no need to set up anything MockInterceptor is
> already there for you but there is no way to access it.
>
> Regards,
> Kris
>
> > > > <mockito-flex%2Bunsu...@googlegroups.com<mockito-flex%252Buns...@googlegroups.com>
> > <mockito-flex%252Buns...@googlegroups.com<mockito-flex%25252Bun...@googlegroups.com>
>
> > > > > > .
> > > > > > For more options, visit this group at
> > > > > >http://groups.google.com/group/mockito-flex?hl=en.
>
> > > > --
> > > > You received this message because you are subscribed to the Google
> > Groups
> > > > "mockito-flex" group.
> > > > To post to this group, send email to mockit...@googlegroups.com.
> > > > To unsubscribe from this group, send email to
> > > > mockito-flex...@googlegroups.com<mockito-flex%2Bunsu...@googlegroups.com>
> > <mockito-flex%2Bunsu...@googlegroups.com<mockito-flex%252Buns...@googlegroups.com>
>
> > > > .
> > > > For more options, visit this group at
> > > >http://groups.google.com/group/mockito-flex?hl=en.
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "mockito-flex" group.
> > To post to this group, send email to mockit...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > mockito-flex...@googlegroups.com<mockito-flex%2Bunsu...@googlegroups.com>
> > .
> > For more options, visit this group at
>
> ...
>
> read more »
Reply all
Reply to author
Forward
0 new messages