freepascal / mockito

Automatically exported from code.google.com/p/mockito
0 stars 0 forks source link

Stubbed call which throws a mocked exception results in a NullPointer #70

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Here is a dummy use case:

when(mock.someFunction()).thenThrow(anotherMock);
when(anotherMock.getRelatedData()).thenReturn(something);
mock.someFunction();

Actual:

java.lang.NullPointerException
    at
org.mockito.exceptions.base.HasStackTraceThrowableWrapper.getStackTrace(HasStack
TraceThrowableWrapper.java:16)
    at
org.mockito.exceptions.base.StackTraceFilter.filterStackTrace(StackTraceFilter.j
ava:20)
    at
org.mockito.internal.stubbing.ThrowsException.answer(ThrowsException.java:23)
    at
org.mockito.internal.stubbing.StubbedInvocationMatcher.answer(StubbedInvocationM
atcher.java:27)
    at org.mockito.internal.MockHandler.intercept(MockHandler.java:93)
    at
org.mockito.internal.creation.MethodInterceptorFilter.intercept(MethodIntercepto
rFilter.java:46)
    at Mock$$EnhancerByMockitoWithCGLIB$$264683d0.someFunction(<generated>)

Expected:

The mock exception is thrown successfully.

What version of the product are you using? On what operating system?
Vista x86, mockito-all-1.7 (maven central).

Background:
The API that I am mocking away considers implementations of the exception
it is throwing to be "details".  Thus the actual exception that is being
thrown is an abstract class.  All implementations of this class are
inaccessible in one way or another.  I was hoping to be able to use mockito
to mock up an implementation without having to implement the class directly.

Original issue reported on code.google.com by nairb...@gmail.com on 7 Apr 2009 at 4:54

GoogleCodeExporter commented 9 years ago
I'm on it. Thanks for reporting!

Original comment by szcze...@gmail.com on 7 Apr 2009 at 8:04

GoogleCodeExporter commented 9 years ago
It's fixed in trunk. 

Mocking throwables is potentially risky because cglib-created Throwable is not
well-created instance (e.g. it does not have stack trace, etc.). Therefore who 
knows
what can happen if you throw it :) I fixed it in trunk anyway.

Original comment by szcze...@gmail.com on 7 Apr 2009 at 8:44

GoogleCodeExporter commented 9 years ago
That was really fast.  For those who has a similar issue, I made the simplest
implementation possible and then used a spy around it to perform the mocking as 
I
would have above.

Again thanks for the fast fix.

Original comment by nairb...@gmail.com on 7 Apr 2009 at 11:51

GoogleCodeExporter commented 9 years ago
This is true - spy() should work because the spied instance is essentially 
correct -
it has the stack trace etc.

Original comment by szcze...@gmail.com on 8 Apr 2009 at 8:59

GoogleCodeExporter commented 9 years ago

Original comment by szcze...@gmail.com on 9 Jun 2009 at 3:15