Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When trying to mock a WeakReference, a StackOverflowError occurs. #2884

Closed
IosephKnecht opened this issue Jan 24, 2023 · 2 comments
Closed

Comments

@IosephKnecht
Copy link

Hi, I ran into the problem of hanging tests when they have mock WeakReference.
The test code looks something like this:

class FooTest {

    @Test
    fun test() {
        val weakReference = Mockito.mock(WeakReference::class.java) as WeakReference<Bar>
        val bar = Mockito.mock(Bar::class.java)

        Mockito.doReturn(bar).`when`(weakReference).get()

        val foo = Foo(weakReference)
        val consumedBar = foo.get()

        Mockito.verify(foo.get())
        Assertions.assertEquals(consumedBar, bar)
    }
}

Is there any way around this, or is it impossible?

Stacktrace
Exception: java.lang.StackOverflowError thrown from the UncaughtExceptionHandler in thread "/127.0.0.1:52766 to /127.0.0.1:52765 workers Thread 2"
Exception: java.lang.StackOverflowError thrown from the UncaughtExceptionHandler in thread "Test worker"
Exception: java.lang.StackOverflowError thrown from the UncaughtExceptionHandler in thread "process reaper"
Exception: java.lang.StackOverflowError thrown from the UncaughtExceptionHandler in thread "/127.0.0.1:52766 to /127.0.0.1:52765 workers Thread 4"
Exception: java.lang.StackOverflowError thrown from the UncaughtExceptionHandler in thread "process reaper"
Exception: java.lang.StackOverflowError thrown from the UncaughtExceptionHandler in thread "/127.0.0.1:52766 to /127.0.0.1:52765 workers Thread 5"
Exception: java.lang.StackOverflowError thrown from the UncaughtExceptionHandler in thread "process reaper"
Exception: java.lang.StackOverflowError thrown from the UncaughtExceptionHandler in thread "/127.0.0.1:52766 to /127.0.0.1:52765 workers Thread 6"
Exception: java.lang.StackOverflowError thrown from the UncaughtExceptionHandler in thread "process reaper"
Exception: java.lang.StackOverflowError thrown from the UncaughtExceptionHandler in thread "/127.0.0.1:52766 to /127.0.0.1:52765 workers Thread 7"
Exception: java.lang.StackOverflowError thrown from the UncaughtExceptionHandler in thread "process reaper"
Exception: java.lang.StackOverflowError thrown from the UncaughtExceptionHandler in thread "/127.0.0.1:52766 to /127.0.0.1:52765 workers Thread 8"
Versions
Mockito core version = 3.12.4
Mockito inline version = 3.12.4
Junit5 version = 1.7.20
OS Version = macOS:12.5.1:21G83
JDK version = openjdk 15.0.2 2021-01-19
OpenJDK Runtime Environment Corretto-15.0.2.7.1 (build 15.0.2+7)
OpenJDK 64-Bit Server VM Corretto-15.0.2.7.1 (build 15.0.2+7, mixed mode, sharing)
Notes

The error cannot be reproduced without connecting mockito-inline, perhaps the problem is org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker.

Full sample

@TimvdLippe
Copy link
Contributor

Unfortunately, WeakReference is one of the classes that Mockito relies on internally for its behavior. Stubbing WeakReference will therefore lead to undefined behavior. Additionally, it is advised not to mock classes you don't own: https://github.com/mockito/mockito/wiki/How-to-write-good-tests#dont-mock-a-type-you-dont-own We are working on improving the user experience by working on a DoNotMock feature to avoid mocking classes/methods that are known to crash Mockito internals (#1833). Therefore, I am closing this as "Infeasible". Apologies for the uninformative exception that is thrown.

@IosephKnecht
Copy link
Author

@TimvdLippe Thanks for the reply.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants