Can I cause an exception to be thrown when a method of a mocked class has not been stubbed?

1,277 views
Skip to first unread message

Robi TidyKiwi

unread,
Dec 17, 2015, 5:16:20 AM12/17/15
to mockito
Is there any way enforcing that any method of a mocked class that is used by the method under test must be stubbed.
At the moment unstubbed methods return null, which may obstensibly be a valid response, but lead to totally irrelevant and misleading when test results.
For me it would be easier if I could specify that Mockito should throw an exception instead - thereby being able to identify tests with unstubbed methods.
Is such a feature available in Mockito?

Robi TidyKiwi

unread,
Dec 17, 2015, 5:16:21 AM12/17/15
to mockito

Brice Dutheil

unread,
Dec 17, 2015, 5:26:30 AM12/17/15
to moc...@googlegroups.com

You can try Mockito.RETURNS_SMART_NULLS, that would be used like mock(A.class, RETURNS_SMART_NULLS).

— Brice

--
You received this message because you are subscribed to the Google Groups "mockito" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mockito+u...@googlegroups.com.
To post to this group, send email to moc...@googlegroups.com.
Visit this group at https://groups.google.com/group/mockito.
To view this discussion on the web visit https://groups.google.com/d/msgid/mockito/0c85685f-af46-435b-8400-383875b3b66d%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Malte Finsterwalder

unread,
Dec 17, 2015, 7:46:49 AM12/17/15
to mockito

Robi TidyKiwi

unread,
Dec 17, 2015, 9:13:43 AM12/17/15
to mockito
Thanks Brice. 
Unfortunately it doesn't do what I need (as far as I can tell) - All that Mockito.RETURNS_SMART_NULLS seems to do is give me a clever not null answer back .eg an empty string if the answer shoulkd be a string (pls correct me if I'm wrong)...

What I need is an exception to be thrown, which causes the test to fail if a method has not been stubbed.

Is there another way to achieve this???

--Robi

Brice Dutheil

unread,
Dec 17, 2015, 9:47:41 AM12/17/15
to moc...@googlegroups.com

Then maybe just use a regular throwing answer :

mock(A.class, (invocation) -> throw new ProgrammerError("forgot to stub that method"));


-- 

Brice Dutheil
--

You received this message because you are subscribed to the Google Groups "mockito" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mockito+u...@googlegroups.com.
To post to this group, send email to moc...@googlegroups.com.
Visit this group at https://groups.google.com/group/mockito.

Robi TidyKiwi

unread,
Dec 17, 2015, 10:02:29 AM12/17/15
to mockito
Thanks Malte

I read, understood and agree the arguments about why expecting the unexpected is not optimal. Howerver, the unexpected tends to get checked in here on a daily basis. Not everybody is as conscientious about getting testing working correctly. Code becomes unreliable and we're not noticing it, because tests don't fail.

I did look at Mockito.verifyNoMoreInteractions, but I'm not sure how that can help my problem.

By the way here's the 'Test' i wrote to try Mockito.RETURNS_SMART_NULLS:

On Thursday, 17 December 2015 11:16:20 UTC+1, Robi TidyKiwi wrote:

Robi TidyKiwi

unread,
Dec 17, 2015, 10:08:57 AM12/17/15
to mockito
But wouldn't that mean puttng a innvocation for all of the methods that weren't stubbed? Which in terms of my question (and what I'm hoping to achieve) is a contradiction in terms... or have I not understood something correctly?

-- Robi


On Thursday, 17 December 2015 11:16:20 UTC+1, Robi TidyKiwi wrote:

Robi TidyKiwi

unread,
Dec 17, 2015, 10:46:18 AM12/17/15
to mockito
Sorry, I didn't attach the file!!!!


On Thursday, 17 December 2015 11:16:20 UTC+1, Robi TidyKiwi wrote:
ThingTest.java

Malte Finsterwalder

unread,
Dec 18, 2015, 7:37:33 AM12/18/15
to mockito
Hi Robi,

you are right, Mockito.verifyNoMoreInteractions does somethings
different. It checks, that all calls to the mock are verified, not
that all calls to the mock are stubbed.

Brice has the right answer, I think.

mock(A.class, (invocation) -> throw new ProgrammerError("forgot to
stub that method"));

The mock-Method has an optional second argument, where you can specify
a default Answer-object. SMART_NULLS is a defaul Answer implementation
provided by mockito, but that does not do what you want. The one given
by Brice should work, though.

Greetings,
Malte

Robi TidyKiwi

unread,
Dec 18, 2015, 9:29:14 AM12/18/15
to mockito
Thanks you Malte and Brice - It took me a while to figure out what you both clearly were stating - Ii works!!!

Thank you very much for your help and your patience

-- Robi


On Thursday, 17 December 2015 11:16:20 UTC+1, Robi TidyKiwi wrote:
Reply all
Reply to author
Forward
0 new messages