praveen1503 / powermock

Automatically exported from code.google.com/p/powermock
Apache License 2.0
0 stars 0 forks source link

UnfinishedStubbingException - add "thenReturn(mock(...))" example #373

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?

1. when(m.foo()).thenReturn(mock(Foo.class));

This correctly results in UnfinishedStubbingException. The message is pretty 
helpful, except it does not call out this case and it can consume a lot of time 
to figure out what is actually wrong:

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!

What is the expected output? What do you see instead?

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. thenReturn(mock(...))
 3. you are trying to stub a final method, you naughty developer!

What version of the product are you using? On what operating system?
1.9.0, Red Hat Linux 5

Original issue reported on code.google.com by kamot...@gmail.com on 23 Feb 2012 at 10:13

GoogleCodeExporter commented 8 years ago
Hi!

This has to do with the behavior of Mockito, not Powermockito.
I refer to a question I recalled from the Mockito FAQ Found here: 
http://code.google.com/p/mockito/wiki/FAQ.

"
Can I thenReturn() an inlined mock() ?
Unfortunately you cannot do this:

  when(m.foo()).thenReturn(mock(Foo.class));
  //                         ^
The reason is that detecting unfinished stubbing wouldn't work if we allow 
above construct. We consider is as a 'trade off' of framework validation (see 
also previous FAQ entry). However you can slightly change the code to make it 
working:

  //extract local variable and start smiling:
  Foo foo = mock(Foo.class);
  when(m.foo()).thenReturn(foo);
"

I hope this answers your question.

Sincerely,

Henrik

Original comment by zherkezhi on 15 May 2012 at 12:57

GoogleCodeExporter commented 8 years ago
My previous post probably was not very clear, basically it would have been 
sufficient to say that as far as I know, this error is from Mockito not 
Powermockito.

Original comment by zherkezhi on 15 May 2012 at 1:03

GoogleCodeExporter commented 8 years ago
Thanks for helping out zherkezhi!

Original comment by johan.ha...@gmail.com on 15 May 2012 at 1:08

GoogleCodeExporter commented 8 years ago
Oh sorry, I did not realize that this is not a Mockito project. Did you just 
close it or move it to the proper place?

Original comment by kamot...@gmail.com on 16 May 2012 at 12:35

GoogleCodeExporter commented 8 years ago
Nevermind, submitted to mockito myself: 
http://code.google.com/p/mockito/issues/detail?id=344

Original comment by kamot...@gmail.com on 27 May 2012 at 3:05