Repository: mockito/mockito
Issue: When custom exception fillInstackTrace() returns null, Exception mock cannot work properly
Labels: bug

```java
public class CustomException extends Exception {

    public CustomException() {
        super();
    }

    @Override
    public Exception fillInStackTrace() {
        return null;
    }
}
```

TestCase like this
```java
 when(demo.process()).thenThrow(new CustomException())

//... test code
```

```
java.lang.NullPointerException
	at org.mockito.internal.exceptions.stacktrace.ConditionalStackTraceFilter.filter(ConditionalStackTraceFilter.java:23)
	at org.mockito.internal.stubbing.answers.ThrowsException.answer(ThrowsException.java:32)
	at org.mockito.internal.stubbing.StubbedInvocationMatcher.answer(StubbedInvocationMatcher.java:35)
	at org.mockito.internal.handler.MockHandlerImpl.handle(MockHandlerImpl.java:95)
```


Issue comments:
This is a bug in the user code. Yet Mockito should handle that gracefully.

Thanks for reporting.

Pull request review comments:
Actually I'd rather make the check in `ConditionalStackTraceFilter.filter()`. And if throwable is null, just return.
Ok! already changed,please review. 
Sorry it seems I was wrong. It seems that indeed your previous attempt was correct.

```java
        if (t == null) {
            throw throwable;
        }
```
Ok, But I don't know why the build is failing?

> Execution failed for task ':assertEnvVariables'.
> Please export 'GH_TOKEN' env variable first. Expression: java.lang.System.env[it]. Values: it = GH_TOKEN
Yes I'm sure your change is correct, it's something else.
Discarding this comment as it requires to rewrite `filter` a bit more.

Patch that resolved the issue:
diff --git a/src/main/java/org/mockito/internal/stubbing/answers/ThrowsException.java b/src/main/java/org/mockito/internal/stubbing/answers/ThrowsException.java
index d323e80b9e..037520fee3 100644
--- a/src/main/java/org/mockito/internal/stubbing/answers/ThrowsException.java
+++ b/src/main/java/org/mockito/internal/stubbing/answers/ThrowsException.java
@@ -29,6 +29,10 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
             throw throwable;
         }
         Throwable t = throwable.fillInStackTrace();
+
+        if (t == null) {
+            throw throwable;
+        }
         filter.filter(t);
         throw t;
     }


Test patch:
diff --git a/src/test/java/org/mockitousage/bugs/NPEWhenCustomExceptionStackTraceReturnNullTest.java b/src/test/java/org/mockitousage/bugs/NPEWhenCustomExceptionStackTraceReturnNullTest.java
new file mode 100644
index 0000000000..50acbad954
--- /dev/null
+++ b/src/test/java/org/mockitousage/bugs/NPEWhenCustomExceptionStackTraceReturnNullTest.java
@@ -0,0 +1,32 @@
+package org.mockitousage.bugs;
+
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockitousage.IMethods;
+import org.mockitoutil.TestBase;
+
+import static junit.framework.TestCase.fail;
+import static org.mockito.Mockito.when;
+
+public class NPEWhenCustomExceptionStackTraceReturnNullTest extends TestBase {
+
+    @Mock
+    IMethods mock;
+
+    class NullStackTraceException extends RuntimeException {
+        @Override
+        public Exception fillInStackTrace() {
+            return null;
+        }
+    }
+
+    //issue 866
+    @Test
+    public void shouldNotThrowNPE() {
+        when(mock.simpleMethod()).thenThrow(new NullStackTraceException());
+        try {
+            mock.simpleMethod();
+            fail();
+        } catch(NullStackTraceException e) {}
+    }
+}