doThrow for any method

6,536 views
Skip to first unread message

Wesley Lira

unread,
May 25, 2012, 3:48:38 PM5/25/12
to mockito
Is possible to execute doThrow for any my stub's method ?

I can compile this

doThrow(new RuntimeException("Ops...")).when(myStub);

But it does not work... I get this exception

org.mockito.exceptions.misusing.UnfinishedStubbingException:
Unfinished stubbing detected here:
-> at com.test..MyTest.doThrowForAnyMethod.java:49)

E.g. thenReturn() may be missing.
Examples of correct stubbing:
when(mock.isOk()).thenReturn(true);
when(mock.isOk()).thenThrow(exception);
doThrow(exception).when(mock).someVoidMethod();
Hints:
1. missing thenReturn()
2. you are trying to stub a final method, you naughty developer!

Any idea ?

David Wallace

unread,
May 26, 2012, 5:54:40 PM5/26/12
to moc...@googlegroups.com
Hi Wesley,
I don't think it's possible to set up a "global stub" that works for any method.  Are you doing this to test exception handling in some class other than the stubbed one?  Or are you just trying to have a test that fails if any of the mock object's methods get called?  If it's the latter, you could look into Mockito's verifyZeroInteractions static method (http://docs.mockito.googlecode.com/hg/latest/org/mockito/Mockito.html#7).
 
Regards,
David.


--
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,
May 26, 2012, 6:00:02 PM5/26/12
to moc...@googlegroups.com
Sorry, Wesley, I'm completely wrong; I wasn't thinking.  Mockito has the concept of a default answer, that you can set up when you create the mock.  All you need to do is write an Answer object that throws an exception, then specify it as the default answer when you make the mock.  This technique is mentioned briefly at http://docs.mockito.googlecode.com/hg/latest/org/mockito/Mockito.html#14 - have a go at getting it to work.  If you have trouble, please post again and I'll supply you with a working example.
 
Regards,
David.

Wesley Lira

unread,
May 30, 2012, 5:25:48 AM5/30/12
to mockito
It's true David!! I've forgot that!
I usually create my mocks with some default Answer, but I've not
thought to create my own answer, thats is perfect to me.

I've asked that because I was mocking a spring's class, RestTemplate,
and I couldn't stub a single method, so I've thought to doThrow for
any method. However I did it right now.
Nevertheless I'll try to create my own answer and post here the
example.

Thank you David!!

On 26 maio, 18:00, David Wallace <dmwallace...@gmail.com> wrote:
> Sorry, Wesley, I'm completely wrong; I wasn't thinking.  Mockito has the
> concept of a default answer, that you can set up when you create the mock.
> All you need to do is write an Answer object that throws an exception, then
> specify it as the default answer when you make the mock.  This technique
> is mentioned briefly athttp://docs.mockito.googlecode.com/hg/latest/org/mockito/Mockito.html#14-
> have a go at getting it to work.  If you have trouble, please post again
> and I'll supply you with a working example.
>
> Regards,
> David.
> On Sun, May 27, 2012 at 9:54 AM, David Wallace <dmwallace...@gmail.com>wrote:
>
>
>
>
>
>
>
> > Hi Wesley,
> > I don't think it's possible to set up a "global stub" that works for any
> > method.  Are you doing this to test exception handling in some class other
> > than the stubbed one?  Or are you just trying to have a test that fails if
> > any of the mock object's methods get called?  If it's the latter, you could
> > look into Mockito's verifyZeroInteractions static method (
> >http://docs.mockito.googlecode.com/hg/latest/org/mockito/Mockito.html#7).
>
> > Regards,
> > David.
>

Wesley Lira

unread,
May 30, 2012, 5:46:48 AM5/30/12
to mockito
Oohh. I can't do that!!

The org.mockito.Answers is an enum, I can't extend it.
There is the class org.mockito.stubbing.Answer, which is used inside
Answers enum.

This topic talks about this subject
https://groups.google.com/group/mockito/browse_thread/thread/db86d0b9df1bec87

My problem was solved, I could do this:

when(restTemplate.postForObject("my.url", null,
String.class)).thenThrow(new RuntimeException("Ops. The post had some
problem."));

My initial problem was that I couldn't use Mockito.anyString() method,
so I've used "my.url".

Thank you for your help David.

On 30 maio, 06:25, Wesley Lira <wesleyl...@gmail.com> wrote:
> It's true David!! I've forgot that!
> I usually create my mocks with some default Answer, but I've not
> thought to create my own answer, thats is perfect to me.
>
> I've asked that because I was mocking a spring's class, RestTemplate,
> and I couldn't stub a single method, so I've thought to doThrow for
> any method. However I did it right now.
> Nevertheless I'll try to create my own answer and post here the
> example.
>
> Thank you David!!
>
> On 26 maio, 18:00, David Wallace <dmwallace...@gmail.com> wrote:
>
>
>
>
>
>
>
> > Sorry, Wesley, I'm completely wrong; I wasn't thinking.  Mockito has the
> > concept of a default answer, that you can set up when you create the mock.
> > All you need to do is write an Answer object that throws an exception, then
> > specify it as the default answer when you make the mock.  This technique
> > is mentioned briefly athttp://docs.mockito.googlecode.com/hg/latest/org/mockito/Mockito.html...

DaveB

unread,
May 30, 2012, 8:39:14 PM5/30/12
to mockito
I'm confused, why couldn't you use the Mockito.anyString() method?

Wesley Lira

unread,
May 31, 2012, 12:21:46 PM5/31/12
to moc...@googlegroups.com
I really don't know!! :(

I know that the method, which I was trying to stub with  Mockito.anyString() method, is orverloaded. 
I think because that, mockito couldn't to use the matcher anyString, but I'm not sure.

Have you ever seen something like that ?

2012/5/30 DaveB <barr...@gmail.com>
--
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.




--
Jesus Cristo é o Senhor. Rm 14:11

Brice Dutheil

unread,
Jun 11, 2012, 7:05:07 AM6/11/12
to moc...@googlegroups.com
Hi Wesley,

When you used anyString, did you replace the other call's arguments by other mockito matchers as well?
If not, Mockito will report a misuse!

Cheers,
-- Brice

Wesley Lira

unread,
Jun 11, 2012, 7:16:09 AM6/11/12
to moc...@googlegroups.com
Hey Brice,

Which call's arguments ??
I just did when(myMock.something(anyString(), stuff)).thenReturn("some string");

I don't know why sometimes I have problem to do that.
Is there something wrong?

Thank you Brice.

2012/6/11 Brice Dutheil <brice....@gmail.com>

Brice Dutheil

unread,
Jun 11, 2012, 7:28:40 AM6/11/12
to moc...@googlegroups.com
Yes, your current use is wrong. As stated somewhere in the javadoc, or in the message of the Mockito exception, you can either use no Mockito matchers at all or use them for all arguments in a single call.

so this line is correct, because no matchers were used :
when(myMock.something("hello world", stuff)).thenReturn("some string"); 

this line is correct as well, because, all Mockito matchers are used for all arguments :
when(myMock.something(anyString(), eq(stuff))).thenReturn("some string");

this line is incorrect, because only one Mockito matcher is used

when(myMock.something(anyString(), stuff)).thenReturn("some string");


The fact is that to detect correctly the matchers, Mockito needs either none or all matchers for a single call.

Cheers,
-- Brice

Wesley Lira

unread,
Jun 11, 2012, 7:43:19 AM6/11/12
to moc...@googlegroups.com
Oohhh.. Ok Brice. I got it!!!

I will try to change my method call to use mockito's matchers for all arguments.
I will let you know the result. :)

Thank you for your explanation!

2012/6/11 Brice Dutheil <brice....@gmail.com>

Wesley Lira

unread,
Jun 11, 2012, 7:47:57 AM6/11/12
to moc...@googlegroups.com
Brice, I had changed my method call.

It works fine!

when(restTemplateMock.getForObject(anyString(), (Class) anyObject())).thenReturn("INVALID JSON");

Thank you guy!!!

2012/6/11 Wesley Lira <wesle...@gmail.com>

Brice Dutheil

unread,
Jun 11, 2012, 7:53:24 AM6/11/12
to moc...@googlegroups.com
You are welcome :)
-- Brice
Reply all
Reply to author
Forward
0 new messages