How to get and print all the args to a mocked method?

620 views
Skip to first unread message

KARR, DAVID (ATTSI)

unread,
Aug 4, 2010, 1:08:07 PM8/4/10
to moc...@googlegroups.com
I'm testing a time-sensitive method. It sometimes fails under certain
conditions, but not when I run the test in debug. One of the objects
I'm mocking is a logger object that the method is using. I'm trying to
verify one of the logger calls, but when I'm troubleshooting the
failure, I'd like to somehow print all the logging data during the test.
I'm aware of the ArgumentCaptor, but this doesn't quite fit this
situation. What are my options?

Malte Finsterwalder

unread,
Aug 4, 2010, 1:36:40 PM8/4/10
to moc...@googlegroups.com
How about building a good old hand made custom class, or at least a
custom wrapper?

Greetings,
Malte

> --
> 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.
>
>

KARR, DAVID (ATTSI)

unread,
Aug 4, 2010, 3:36:22 PM8/4/10
to moc...@googlegroups.com
> -----Original Message-----
> From: moc...@googlegroups.com [mailto:moc...@googlegroups.com] On
> Behalf Of Malte Finsterwalder
> Sent: Wednesday, August 04, 2010 10:37 AM
> To: moc...@googlegroups.com
> Subject: Re: [mockito] How to get and print all the args to a mocked
> method?
>
> How about building a good old hand made custom class, or at least a
> custom wrapper?

Well, that's a good idea, but I don't think it really helps here. I still want the object mocked, so I can verify its behavior, but I also want to get the arguments that were sent in each call to it, so I can temporarily print out the values.

It's almost like I want an interceptor/advice on the mock, where the interceptor does whatever it wants with the arguments, and would just print them out in my case.

Igor Czechowski

unread,
Aug 4, 2010, 3:38:51 PM8/4/10
to moc...@googlegroups.com

Wouldn't Mockito.spy solve your problem ?

Igor

On Aug 4, 2010 9:36 PM, "KARR, DAVID (ATTSI)" <dk0...@att.com> wrote:

> -----Original Message-----
> From: moc...@googlegroups.com [mailto:moc...@googlegroups.com] On
>...

Well, that's a good idea, but I don't think it really helps here.  I still want the object mocked, so I can verify its behavior, but I also want to get the arguments that were sent in each call to it, so I can temporarily print out the values.

It's almost like I want an interceptor/advice on the mock, where the interceptor does whatever it wants with the arguments, and would just print them out in my case.


> On 4 August 2010 19:08, KARR, DAVID (ATTSI) <dk0...@att.com> wrote:

> > I'm testing a time-sensit...

--

You received this message because you are subscribed to the Google Groups "mockito" group.

To post t...

Malte Finsterwalder

unread,
Aug 5, 2010, 4:16:51 AM8/5/10
to moc...@googlegroups.com
>> How about building a good old hand made custom class, or at least a
>> custom wrapper?
>
> Well, that's a good idea, but I don't think it really helps here.  I still want the object mocked, so I can verify its behavior, but I also want to get the arguments that were sent in each call to it, so I can temporarily print out the values.
>
> It's almost like I want an interceptor/advice on the mock, where the interceptor does whatever it wants with the arguments, and would just print them out in my case.

You can do so with a custom wrapper, I think.
Just implement your interface with methods that print the arguments
which then delegate internally to a mock object.
If you want to make it more clever, you could probably write a generic
dynamic proxy that does the same thing.

Greetings,
Malte

szczepiq

unread,
Aug 10, 2010, 10:59:00 AM8/10/10
to moc...@googlegroups.com
Hey

Your options are:

- patching Mockito: http://code.google.com/p/mockito/issues/detail?id=148
- create hand mock and spy it
- (or as Malte suggests) create a hand mock that delegates to Mockito mock
- verifyNoMoreInteractions() method prints all interactions when failed.

Cheers,
Szczepan

Reply all
Reply to author
Forward
0 new messages