Description
Mockito tests running on Java 8 (JDK 1.8) crash with an ArrayStoreException / EnumConstantNotPresentExceptionProxy when attempting to mock generated Stub or Client classes that contain JSpecify annotations (@NullMarked or @nullable).
Because JSpecify uses Type-Use (TYPE_USE) annotations, it triggers a known limitation in standard JDK 8 Java Reflection API. Standard JVM annotation parsers throw an ArrayStoreException when bytecode scanners (like ByteBuddy, underneath Mockito) introspect parameterized types, arrays, or generic descriptors holding Type-Use annotations.
Workaround
Handwritten tests aiming to maintain backwards compatibility with Java 8 while using JSpecify must explicitly disable annotation processing on mocked generated classes. Instead of standard @mock Declaration annotations, instantiations must use:
Mockito.mock(MyStub.class, Mockito.withSettings().withoutAnnotations());.
This workaround has been successfully applied in the following integration PRs:
#13702
#13612
#13621
Reproducer
Steps to reproduce:
- Generate GAPIC veneers/stubs annotated with JSpecify @NullMarked or @nullable (Type-Use).
- Write a handwritten unit test using Java 8 runtime.
- Instantiate standard Mockito mock for the generated class: MetricServiceStub mockStub = Mockito.mock(MetricServiceStub.class); or @mock private MetricServiceStub mockStub;.
- Run tests and observe ArrayStoreException crash.
Code Snippets:
Failing Snippet (Standard/Declaration):
@mock private MetricServiceStub mockMetricServiceStub; // Crashes on JDK 8
Bypass/Working Snippet (Programmatic without annotations):
private MetricServiceStub mockMetricServiceStub;
@before
public void setUp() {
// Bypasses reflection parser crash
mockMetricServiceStub = Mockito.mock(
MetricServiceStub.class,
Mockito.withSettings().withoutAnnotations()
);
}
Logs and Stack Trace
org.mockito.exceptions.base.MockitoException:
Mockito cannot mock this class: class com.google.cloud.monitoring.v3.stub.MetricServiceStub.
Mockito can only mock non-private & non-final classes, but the root cause of this error might be different.
...
Caused by: java.lang.ArrayStoreException: sun.reflect.annotation.EnumConstantNotPresentExceptionProxy
at java.lang.Class.createAnnotationData(Class.java:3521)
at java.lang.Class.annotationData(Class.java:3510)
at java.lang.Class.getDeclaredAnnotations(Class.java:3477)
Behavior
When did the issue begin? Began following PR integrations introducing JSpecify Type-Use annotations (@NullMarked, @nullable) to generated surfaces.
Is this behavior flaky? No, it is consistently seen during the Maven Testing Phase running specifically on JDK 1.8.
Description
Mockito tests running on Java 8 (JDK 1.8) crash with an ArrayStoreException / EnumConstantNotPresentExceptionProxy when attempting to mock generated Stub or Client classes that contain JSpecify annotations (@NullMarked or @nullable).
Because JSpecify uses Type-Use (TYPE_USE) annotations, it triggers a known limitation in standard JDK 8 Java Reflection API. Standard JVM annotation parsers throw an ArrayStoreException when bytecode scanners (like ByteBuddy, underneath Mockito) introspect parameterized types, arrays, or generic descriptors holding Type-Use annotations.
Workaround
Handwritten tests aiming to maintain backwards compatibility with Java 8 while using JSpecify must explicitly disable annotation processing on mocked generated classes. Instead of standard @mock Declaration annotations, instantiations must use:
Mockito.mock(MyStub.class, Mockito.withSettings().withoutAnnotations());.
This workaround has been successfully applied in the following integration PRs:
#13702
#13612
#13621
Reproducer
Steps to reproduce:
Code Snippets:
Failing Snippet (Standard/Declaration):
@mock private MetricServiceStub mockMetricServiceStub; // Crashes on JDK 8
Bypass/Working Snippet (Programmatic without annotations):
private MetricServiceStub mockMetricServiceStub;
@before
public void setUp() {
// Bypasses reflection parser crash
mockMetricServiceStub = Mockito.mock(
MetricServiceStub.class,
Mockito.withSettings().withoutAnnotations()
);
}
Logs and Stack Trace
org.mockito.exceptions.base.MockitoException:
Mockito cannot mock this class: class com.google.cloud.monitoring.v3.stub.MetricServiceStub.
Mockito can only mock non-private & non-final classes, but the root cause of this error might be different.
...
Caused by: java.lang.ArrayStoreException: sun.reflect.annotation.EnumConstantNotPresentExceptionProxy
at java.lang.Class.createAnnotationData(Class.java:3521)
at java.lang.Class.annotationData(Class.java:3510)
at java.lang.Class.getDeclaredAnnotations(Class.java:3477)
Behavior
When did the issue begin? Began following PR integrations introducing JSpecify Type-Use annotations (@NullMarked, @nullable) to generated surfaces.
Is this behavior flaky? No, it is consistently seen during the Maven Testing Phase running specifically on JDK 1.8.