mockito/mockito · error · MockitoSerializationIssue

Wow, the class 'ObjectStreamClass' in the JDK don't have the

Error message

Wow, the class 'ObjectStreamClass' in the JDK don't have the field 'name',
this is definitely a bug in our code as it means the JDK team changed a few internal things.

Please report an issue with the JDK used, a code sample and a link to download the JDK would be welcome.

What it means

Mockito's cross-class-loader serialization support reflects into the JDK-internal class java.io.ObjectStreamClass to overwrite its private 'name' field so a deserialized proxy class matches the newly created class. If reflection reports NoSuchFieldException, the JDK's internal layout changed, so this hack no longer works. Mockito throws MockitoSerializationIssue because this is considered a JDK incompatibility bug rather than a user error.

Source

Thrown at mockito-core/src/main/java/org/mockito/internal/creation/bytebuddy/ByteBuddyCrossClassLoaderSerializationSupport.java:340

         * the given <code>ObjectStreamClass</code> to change the name with the newly created class.
         *
         * @param descInstance The <code>ObjectStreamClass</code> that will be hacked.
         * @param proxyClass   The proxy class whose name will be applied.
         * @throws java.io.InvalidObjectException
         */
        private void hackClassNameToMatchNewlyCreatedClass(
                ObjectStreamClass descInstance, Class<?> proxyClass) throws ObjectStreamException {
            try {
                MemberAccessor accessor = Plugins.getMemberAccessor();
                Field classNameField = descInstance.getClass().getDeclaredField("name");
                try {
                    accessor.set(classNameField, descInstance, proxyClass.getCanonicalName());
                } catch (IllegalAccessException e) {
                    throw new MockitoSerializationIssue(
                            "Access to " + classNameField + " was denied", e);
                }
            } catch (NoSuchFieldException nsfe) {
                throw new MockitoSerializationIssue(
                        join(
                                "Wow, the class 'ObjectStreamClass' in the JDK don't have the field 'name',",
                                "this is definitely a bug in our code as it means the JDK team changed a few internal things.",
                                "",
                                "Please report an issue with the JDK used, a code sample and a link to download the JDK would be welcome."),
                        nsfe);
            }
        }

        /**
         * Read the stream class annotation and identify it as a Mockito mock or not.
         *
         * @param marker The marker to identify.
         * @return <code>true</code> if not marked as a Mockito, <code>false</code> if the class annotation marks a Mockito mock.
         */
        private boolean notMarkedAsAMockitoMock(Object marker) {
            return !MOCKITO_PROXY_MARKER.equals(marker);
        }

View on GitHub (pinned to 5a676bcd9e)

Solutions

  1. Check Mockito compatibility with your JDK version and upgrade Mockito to a release supporting your JDK
  2. Report an issue to Mockito with the JDK version, a code sample, and a download link for the JDK
  3. As a workaround, avoid cross-class-loader mock serialization (keep mock creation and deserialization in the same class loader)
  4. Pin to a JDK version where ObjectStreamClass still exposes the 'name' field
Defensive patterns

Strategy: try-catch

Try / catch

try {
    // deserialize mock across class loaders
} catch (MockitoSerializationIssue e) {
    // JDK internals incompatible: log JDK vendor/version, fall back to non-cross-loader usage
}

Prevention

When it happens

Trigger: Deserializing a mock via Mockito's cross-class-loader serialization path when hackClassNameToMatchNewlyCreatedClass (called from resolveClass) looks up ObjectStreamClass.getDeclaredField("name") and the field is missing — i.e. running on a JDK where ObjectStreamClass no longer has the 'name' field.

Common situations: Running Mockito on a newer/exotic JDK build where java.io.ObjectStreamClass internals were refactored; using a non-OpenJDK JVM vendor; forward-compatibility break after a JDK upgrade; custom instrumentation/agents that redefine ObjectStreamClass.

Related errors


AI-assisted analysis of mockito/mockito@5a676bcd9e (2026-09-05). Data as JSON: /api/errors/ef9469805e13c87e. Report an issue: GitHub.