Repository: mockito/mockito
Issue: StackOverflowError in MockitoSerializationIssue.java
Labels: bug

In MockitoSerializationIssue.filterStackTrace(), it calls `filter.filter(this)`, which then calls getStackTrace() again, causing an infinite recursion.


Issue comments:
Well spotted. We'll fix it. Any chance for PR?

Sorry I should really have sent a PR with the bug report.

I'm recently pretty swamped. Spent a day and an evening trying to upgrade Mockito (thought it was a low-hanging fruit) but then had to draw the line because fixing all the breakage in Google's codebase had started to look like a sizable project.

Thanks a lot for the reports. This is great stuff. Thank you for pushing
mockito upgrade at G. This helps a lot because you have already fleshed out
a couple of bugs.

On Thu, Jul 2, 2015, 12:03 Ben Yu notifications@github.com wrote:

> Sorry I should really have sent a PR with the bug report.
> 
> I'm recently pretty swamped. Spent a day and an evening trying to upgrade
> Mockito (thought it was a low-hanging fruit) but then had to draw the line
> because fixing all the breakage in Google's codebase had started to look
> like a sizable project.
> 
> —
> Reply to this email directly or view it on GitHub
> https://github.com/mockito/mockito/issues/244#issuecomment-118131389.

Closed via fa0ebda114afefedea340077b60bc1d8474c7767


Pull request review comments:

Patch that resolved the issue:
diff --git a/src/org/mockito/exceptions/base/MockitoSerializationIssue.java b/src/org/mockito/exceptions/base/MockitoSerializationIssue.java
index 67a7387016..458b56dcc6 100644
--- a/src/org/mockito/exceptions/base/MockitoSerializationIssue.java
+++ b/src/org/mockito/exceptions/base/MockitoSerializationIssue.java
@@ -30,12 +30,6 @@ public MockitoSerializationIssue(String message, Exception cause) {
         filterStackTrace();
     }
 
-    @Override
-    public StackTraceElement[] getStackTrace() {
-        filterStackTrace();
-        return super.getStackTrace();
-    }
-
     private void filterStackTrace() {
         unfilteredStackTrace = super.getStackTrace();
 


Test patch:
diff --git a/test/org/mockito/exceptions/base/MockitoSerializationIssueTest.java b/test/org/mockito/exceptions/base/MockitoSerializationIssueTest.java
new file mode 100644
index 0000000000..8e81b00939
--- /dev/null
+++ b/test/org/mockito/exceptions/base/MockitoSerializationIssueTest.java
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2007 Mockito contributors
+ * This program is made available under the terms of the MIT License.
+ */
+
+package org.mockito.exceptions.base;
+
+import org.junit.Test;
+import org.mockito.internal.configuration.ConfigurationAccess;
+import org.mockitoutil.TestBase;
+
+import java.util.Arrays;
+
+public class MockitoSerializationIssueTest extends TestBase {
+
+    @Test
+    public void shouldFilterOutTestClassFromStacktraceWhenCleanFlagIsTrue() {
+        // given
+        ConfigurationAccess.getConfig().overrideCleansStackTrace(true);
+
+        // when
+        MockitoSerializationIssue issue = new MockitoSerializationIssue("msg", new Exception("cause"));
+
+        // then
+        assertContains("MockitoSerializationIssueTest", Arrays.toString(issue.getUnfilteredStackTrace()));
+        assertNotContains("MockitoSerializationIssueTest", Arrays.toString(issue.getStackTrace()));
+    }
+
+    @Test
+    public void shouldKeepExecutingClassInStacktraceWhenCleanFlagIsFalse() {
+        // given
+        ConfigurationAccess.getConfig().overrideCleansStackTrace(false);
+
+        // when
+        MockitoSerializationIssue issue = new MockitoSerializationIssue("msg", new Exception("cause"));
+
+        // then
+        assertContains("MockitoSerializationIssueTest", Arrays.toString(issue.getUnfilteredStackTrace()));
+        assertContains("MockitoSerializationIssueTest", Arrays.toString(issue.getStackTrace()));
+    }
+}