Closed GoogleCodeExporter closed 9 years ago
Sorry, missed a line in testCar(), which should be:
honda = new Honda();
honda.setName(null);
Mockito.when(honda.getType()).thenReturn("Honda");
Original comment by hzhu028@gmail.com
on 20 Apr 2009 at 1:28
But in this case honda is not a mock, but a concrete object -> honda = new
Honda();
honda.getType() is called on a real instance and name is null, so that's why
you're
getting NPE. Unless I'm missing something.
Original comment by bbankow...@gmail.com
on 20 Apr 2009 at 5:44
Ok, lets say:
// honda = new Honda();
// honda.setName(null);
Mockito.when(honda.getType()).thenReturn("Honda");
this is still a null pointer.
Original comment by hzhu028@gmail.com
on 20 Apr 2009 at 6:04
Where do you actually create your mock? I can see @Mock annotation, but
MockitoAnnotations.initMocks(this) is missing. If I add this line there is no
NPE.
Please, let us know if it helps.
Original comment by bbankow...@gmail.com
on 20 Apr 2009 at 7:51
Thank you for your reply!
And, sorry, assume that I have MockitoAnnotations.initMocks(this) in the
setUp()
method so that it always runs.
I didn't quite exactly remember the situation where I had the problem in my
real
production code, the above was a example trying to reproduce the problem
(obviously
not a good example though ;P).
How about changing the getType() from Car to be final? This time, I got a NPE
when I
run Mockito.when(honda.getType()).thenReturn("Honda");. Is that supposed to be
that
Mockito cannot mock a final method? Or I missed something? - If so, how can I
mock a
final method like in the above example?
Thanks very much for your time!
Original comment by hzhu028@gmail.com
on 20 Apr 2009 at 12:47
Unfortunately Mockito cannot mock final methods/classes. You can check it in
FAQ:
http://code.google.com/p/mockito/wiki/FAQ
Original comment by bbankow...@gmail.com
on 20 Apr 2009 at 12:59
Ok, I'm closing this one - it seems not a bug.
Original comment by szcze...@gmail.com
on 20 Apr 2009 at 7:35
Original issue reported on code.google.com by
hzhu028@gmail.com
on 20 Apr 2009 at 1:24