Resetting interactions but not stubbing

2,934 views
Skip to first unread message

Tor Pedersen

unread,
Feb 9, 2012, 2:32:00 PM2/9/12
to mockito
I'm trying to test a project which uses guice injection, I have
created a test module to let me inject some mocks instead of the data
access object, mail service, etc, while testing the other classes
"managed" by guice. I have split the tests into many small, simple and
focused @Test methods (using junit 4), each testing different simple
scenarios, but all are part of the same test class. I'm looking for
(but haven't found) a way to reset the recorded interactions on mock
objects between each test method (in my @Before method) without
removing all the stubs for the mock object (itself injected into a non-
mock singleton managed by guice).

Is there a secret way to do this? If not, should this be implemented
at some point? Or does my code smell?

Tor

Brice Dutheil

unread,
Feb 9, 2012, 4:58:01 PM2/9/12
to moc...@googlegroups.com
Hi Tor,

Yeah the secret way is hidden in the internals of mockito. We don't have plan for that, please fill an issue if you think it should be added. 

I'm not sur to visualize how you structured your code, also I'm not familiar with GUice. Actually the way I'm coding stuff makes no sense to reset the verification, I'd rather have focused tests with custom stubs or have some defaults in the before block. Though your use case might be legitimate, this almost looks like integration tests. So this might be useful for others.
So if you try an implementation you might want to take a look at how Mockito.reset and Mockito.verify behave.


David / Szczepan what do you think ?

-- Brice




Tor

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


David Wallace

unread,
Feb 9, 2012, 5:37:34 PM2/9/12
to moc...@googlegroups.com
When I first encountered reset(), I was surprised that it would reset stubbing information as well as verification information; and that there appeared to be no way to reset one or the other. 
 
I could easily imagine a case where you're doing performance tests of some component of your system, and you need to reset the verification information of a mock, but don't want to reset the stubbing.  In fact, this is (distantly) related to my ongoing bugbear - issue 84. 
 
I don't quite understand what Tor is trying to do, so I can't comment on whether there's a code smell going on.  But if Brice and Szczepan feel that having a way of resetting the verification information would be a good thing to have, I would support this 100%.  Possibly even to the extent of implementing it :-)
 
Cheers,
David.

Brice Dutheil

unread,
Feb 10, 2012, 3:36:57 AM2/10/12
to moc...@googlegroups.com
Actually for performance test I would even avoid recording interactions, instead of reseting them.

However I think Tor need to check interactions before reseting them.

About implementing it, I don't have yet anything against it. Free time is scarce for me these past months so if some of you want to hack in...




-- Brice

David Wallace

unread,
Feb 10, 2012, 5:30:31 AM2/10/12
to moc...@googlegroups.com
Yes, Brice, re the performance test, that's EXACTLY what I want.  And I believe David Gageot provided a patch back in 2010 that would allow you to create a mock that doesn't record its interactions.  See issue 84 for details.  But I think either you or Szczepan had some reservations about adding David Gageot's patch.  I never quite understood what those reservations were; and I would love to have this patch in the next version of Mockito.
 
To meet Tor's requirement though of a reset method that only resets the interactions, I'd be happy to work on this.  Tor, can you please raise an issue on the issues list?
 
Regards,
David. 

Tor Pedersen

unread,
Feb 11, 2012, 5:20:52 AM2/11/12
to mockito
Ok... submitted it as issue 316, along with a (hopefully
understandable) explanation of why it would be a useful feature...

Tor

On 10 Feb, 11:30, "David Wallace" <dmwallace...@gmail.com> wrote:
> Yes, Brice, re the performance test, that's EXACTLY what I want.  And I believe David Gageot provided a patch back in 2010 that would allow you to create a mock that doesn't record its interactions.  See issue 84 for details.  But I think either you or Szczepan had some reservations about adding David Gageot's patch.  I never quite understood what those reservations were; and I would love to have this patch in the next version of Mockito.
>
> To meet Tor's requirement though of a reset method that only resets the interactions, I'd be happy to work on this.  Tor, can you please raise an issue on the issues list?
>
> Regards,
> David.
>
>
>
>
>
>
>
>   ----- Original Message -----
>   From: Brice Dutheil
>   To: moc...@googlegroups.com
>   Sent: Friday, February 10, 2012 9:36 PM
>   Subject: Re: [mockito] Resetting interactions but not stubbing
>
>   Actually for performance test I would even avoid recording interactions, instead of reseting them.
>
>   However I think Tor need to check interactions before reseting them.
>
>   About implementing it, I don't have yet anything against it. Free time is scarce for me these past months so if some of you want to hack in...
>
>   -- Brice
>
>   On Thu, Feb 9, 2012 at 23:37, David Wallace <dmwallace...@gmail.com> wrote:
>
>     When I first encountered reset(), I was surprised that it would reset stubbing information as well as verification information; and that there appeared to be no way to reset one or the other.
>
>     I could easily imagine a case where you're doing performance tests of some component of your system, and you need to reset the verification information of a mock, but don't want to reset the stubbing.  In fact, this is (distantly) related to my ongoing bugbear - issue 84.
>
>     I don't quite understand what Tor is trying to do, so I can't comment on whether there's a code smell going on.  But if Brice and Szczepan feel that having a way of resetting the verification information would be a good thing to have, I would support this 100%.  Possibly even to the extent of implementing it :-)
>
>     Cheers,
>     David.
>
>     On Fri, Feb 10, 2012 at 10:58 AM, Brice Dutheil <brice.duth...@gmail.com> wrote:
>
>       Hi Tor,
>
>       Yeah the secret way is hidden in the internals of mockito. We don't have plan for that, please fill an issue if you think it should be added.
>
>       I'm not sur to visualize how you structured your code, also I'm not familiar with GUice. Actually the way I'm coding stuff makes no sense to reset the verification, I'd rather have focused tests with custom stubs or have some defaults in the before block. Though your use case might be legitimate, this almost looks like integration tests. So this might be useful for others.
>       So if you try an implementation you might want to take a look at how Mockito.reset and Mockito.verify behave.
>
>       David / Szczepan what do you think ?
>
>       -- Brice
>
>       On Thu, Feb 9, 2012 at 20:32, Tor Pedersen <dr.torpeder...@gmail.com> wrote:
>
>         I'm trying to test a project which uses guice injection, I  have
>         created a test module to let me inject some mocks instead of the data
>         access object, mail service, etc, while testing the other classes
>         "managed" by guice. I have split the tests into many small, simple and
>         focused @Test methods (using junit 4), each testing different simple
>         scenarios, but all are part of the same test class. I'm looking for
>         (but haven't found) a way to reset the recorded interactions on mock
>         objects between each test method (in my @Before method) without
>         removing all the stubs for the mock object (itself injected into a non-
>         mock singleton managed by guice).
>
>         Is there a secret way to do this? If not, should this be implemented
>         at some point? Or does my code smell?
>
>         Tor
>
>         --
>         You received this message because you are subscribed to the Google Groups "mockito" group.
>         To post to this group, send email to moc...@googlegroups.com.
>         To unsubscribe from this group, send email to mockito+u...@googlegroups.com.
>         For more options, visit this group athttp://groups.google.com/group/mockito?hl=en.
>
>       --
>       You received this message because you are subscribed to the Google Groups "mockito" group.
>       To post to this group, send email to moc...@googlegroups.com.
>       To unsubscribe from this group, send email to mockito+u...@googlegroups.com.
>       For more options, visit this group athttp://groups.google.com/group/mockito?hl=en.
>
>     --
>     You received this message because you are subscribed to the Google Groups "mockito" group.
>     To post to this group, send email to moc...@googlegroups.com.
>     To unsubscribe from this group, send email to mockito+u...@googlegroups.com.
>     For more options, visit this group athttp://groups.google.com/group/mockito?hl=en.

Szczepan Faber

unread,
Feb 11, 2012, 10:01:21 AM2/11/12
to moc...@googlegroups.com
> But I think either you or Szczepan had some reservations about adding David Gageot's patch. 

As far as I remember we fixed the problem slightly differently and it did help to resolve the original use case. So basically adding the non-recording kind of mock became not necessary.

Anyways, both feature requests (resetting excluding stubs and mock that don't remember interactions make sense to me). I do have some initial thoughts related to implementation of both features :)

Cheers
Szczepan Faber
Principal engineer@gradleware
Lead@mockito
Reply all
Reply to author
Forward
0 new messages