Skip to content

StackOverflowError when mocking WeakReference #3758

@jselbo

Description

@jselbo

Setup:

  • JDK 21
  • Mockito 5.20.0

Trying to mock a WeakReference leads to a StackOverflowError:

java.lang.StackOverflowError
at org.mockito.internal.creation.bytebuddy.InlineDelegateByteBuddyMockMaker.lambda$new$2(InlineDelegateByteBuddyMockMaker.java:288)
at org.mockito.internal.creation.bytebuddy.MockMethodAdvice.isConstructorMock(MockMethodAdvice.java:207)
at org.mockito.internal.creation.bytebuddy.inject.MockMethodDispatcher.isConstructorMock(MockMethodDispatcher.java:57)
at java.base/java.lang.ref.WeakReference.<init>(WeakReference.java)
at java.base/java.lang.ThreadLocal$ThreadLocalMap$Entry.<init>(ThreadLocal.java:386)
at java.base/java.lang.ThreadLocal$ThreadLocalMap.set(ThreadLocal.java:563)
at java.base/java.lang.ThreadLocal.setInitialValue(ThreadLocal.java:228)
at java.base/java.lang.ThreadLocal.get(ThreadLocal.java:194)
at java.base/java.lang.ThreadLocal.get(ThreadLocal.java:172)
( repeats )

With gradle the failure is a bit obscured but this manifests in the report as:

> Task :mockito-core:test

Exception: java.lang.StackOverflowError thrown from the UncaughtExceptionHandler in thread "Logging-Cleaner"

Exception: java.lang.StackOverflowError thrown from the UncaughtExceptionHandler in thread "Thread-0"

> Task :mockito-core:test FAILED
  org.mockito.internal.creation.bytebuddy.InlineDelegateByteBuddyMockMakerTest - my_test_that_mocks_WeakReference

  32 passing (2.7s)
  1 pending

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions