Skip to content

mockStatic fails with class redefinition failed: invalid class #3273

Open
@scordio

Description

Mockito message in the stack trace:

java.lang.InternalError: class redefinition failed: invalid class

	at java.instrument/sun.instrument.InstrumentationImpl.retransformClasses0(Native Method)
	at java.instrument/sun.instrument.InstrumentationImpl.retransformClasses(InstrumentationImpl.java:167)
	at org.mockito.internal.creation.bytebuddy.InlineBytecodeGenerator.triggerRetransformation(InlineBytecodeGenerator.java:281)
	at org.mockito.internal.creation.bytebuddy.InlineBytecodeGenerator.mockClassStatic(InlineBytecodeGenerator.java:226)
	at org.mockito.internal.creation.bytebuddy.TypeCachingBytecodeGenerator.mockClassStatic(TypeCachingBytecodeGenerator.java:108)
	at org.mockito.internal.creation.bytebuddy.InlineDelegateByteBuddyMockMaker.createStaticMock(InlineDelegateByteBuddyMockMaker.java:592)
	at org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker.createStaticMock(InlineByteBuddyMockMaker.java:83)
	at org.mockito.internal.util.MockUtil.createStaticMock(MockUtil.java:202)
	at org.mockito.internal.MockitoCore.mockStatic(MockitoCore.java:134)
	at org.mockito.Mockito.mockStatic(Mockito.java:2328)
	at org.mockito.Mockito.mockStatic(Mockito.java:2265)
	at <line of try block in test method>

The problematic class belongs to the Thales JCPROV library which is unfortunately not available on Maven Central so I cannot compose a reproducer.

However, I can describe its characteristics:

  • Public, top level and non-final
  • With a default constructor that is private and does nothing
  • With public methods that are all static and native
  • With a static initialization block that loads a native library via System.loadLibrary

As the native library is not available during test execution (and I cannot make it available), I would guess that the failure in the static block triggers the exception above. However, I don't know how to prove it.

Is there anything I could do to properly mock such a class? Otherwise, do you see any possible improvements in Mockito for these cases?

Please, let me know in case I could grab additional details to help the analysis.

The issue happens on Mockito 5.10.0 and OpenJDK 11.0.12.

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