[Testing] Mockito fails with ArrayStoreException on JDK 8 due to JSpecify TYPE_USE annotations
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 35/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Cần làm rõ
- Mức độ hoạt động
- Ít trao đổi
- Công nghệ
- java
- Lĩnh vực
- testing-qa
Hướng nghiên cứu
Bắt đầu bằng cách chạy giai đoạn kiểm thử của Maven trên JDK 8 với một lớp Stub hoặc Client được tạo ra sử dụng các annotation JSpecify và một Mockito mock tiêu chuẩn. So sánh mock dựa trên khai báo bị lỗi với Mockito.mock(..., Mockito.withSettings().withoutAnnotations()). Issue không nêu tệp đích hoặc tiêu chí nghiệm thu nào, vì vậy cần làm rõ bản sửa được dự định và definition of done.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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:
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.
- Ngôn ngữ chính
- Java
- Star
- 2.1k
- Fork
- 1.2k
- Merge trung bình
- 2 ngày 14 giờ
- Pull request đã merge (30 ngày)
- 192
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của googleapis/google-cloud-java
-
[java-core] Timestamp.of(java.sql.Timestamp) is one second late for a time less than a second before the epochCó thể đã có người làm @Vcode2407 đã nhận 4 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
googleapis/google-cloud-java#14636 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[java-firestore] UserDataConverter encodes a pre-epoch Date with negative nanos, so the write is refusedCó thể đã có người làm @wu-hui đã nhận 23 ngày trước. Đang mởpriority: p2 type: bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
googleapis/google-cloud-java#14635 · 1 người được giao ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[java-firestore] UserDataConverter throws IndexOutOfBoundsException on an empty __binary__ valueCó thể đã có người làm @wu-hui đã nhận 23 ngày trước. Đang mởpriority: p2 type: bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
googleapis/google-cloud-java#14634 · 1 bình luận · 1 người được giao ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[java-firestore] CustomClassMapper.convertBigDecimal calls .abs() on Double, unintentionally flipping negative values to positiveCó thể làm lại được @wu-hui đã nhận 45 ngày trước và không có pull request nào đang mở. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 85/100
googleapis/google-cloud-java#14533 · 1 người được giao ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Github CI failing when trying to install xmllintCó thể đã có người làm @blakeli0 đã nhận 81 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
googleapis/google-cloud-java#14132 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của googleapis/google-cloud-java
Issue tương tự
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 88/100
-
Độ khó 2/5 Dưới một giờ Mức phù hợp với người mới 84/100
cpp-best-practices/project_validator#251 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
backend enhancement
Độ khó 2/5 Nửa ngày Mức phù hợp với người mới 70/100
damodharg6/payledger#16 ·
-
[Bug]: An initiative list with no current entry logs an error on every changeCó thể đã có người làm @dwizard0 đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 85/100
Maintainer thường phản hồi trong vòng 1 ngày